- Author:
- rubic
- Posted:
- February 26, 2007
- Language:
- Python
- Version:
- Pre .96
- Score:
- 4 (after 4 ratings)
DynamicFieldSnippetForm demonstrates how to dynamically assign fields in newforms.
1. weight is a required static field
2. height is an optional dynamic field
This example uses request_height
as an optional keyword argument to declare whether the height
field should be added to the form, but it's just there for demonstration purposes. If you decide to use a keyword argument in your code, be sure to pop it off (as demonstrated in the code) or you'll get an unexpected keyword argument error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from django import newforms as forms
class DynamicFieldSnippetForm(forms.Form):
"""DynamicFieldSnippetForm - declare a field dynamically in a form.
The weight field is required. The height field is optional and
not included on the form unless requested.
>>> print DynamicFieldSnippetForm()
<tr><th><label for="id_weight">Weight:</label></th><td><input type="text" name="weight" id="id_weight" /></td></tr>
>>> print DynamicFieldSnippetForm(request_height=True)
<tr><th><label for="id_weight">Weight:</label></th><td><input type="text" name="weight" id="id_weight" /></td></tr>
<tr><th><label for="id_height">Height:</label></th><td><input type="text" name="height" id="id_height" /></td></tr>
>>> print DynamicFieldSnippetForm({'height':174, 'weight':122}, request_height=True)
<tr><th><label for="id_weight">Weight:</label></th><td><input type="text" name="weight" value="122" id="id_weight" /></td></tr>
<tr><th><label for="id_height">Height:</label></th><td><input type="text" name="height" value="174" id="id_height" /></td></tr>
>>>
"""
def __init__(self, *args, **kwargs):
request_height = kwargs.pop('request_height', False)
super(DynamicFieldSnippetForm, self).__init__(*args, **kwargs)
if request_height:
self.fields['height'] = forms.CharField(required=False)
weight = forms.CharField()
if __name__ == '__main__':
import doctest
doctest.testmod()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
If you are also giving dynamic names to your dynamic fields and thusly need to generate dynamic
clean_FOO()
methods, the following lambda trick may give you a hint on how to proceed:#
There is, and it's called closures. Not much shorter, but certainly more readable.
#
Please login first before commenting.