Login

assign fields dynamically in newforms

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

  1. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

whiteinge (on June 11, 2007):

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:

setattr(self, 'clean_%s' % FOO, lambda: self._clean_FOO_helper(FOO))

def _clean_FOO_helper(self, your_field_name):
    # normal raise validation error stuff goes here
    return self.cleaned_data.get(field_name)

#

whiteinge (on September 17, 2007):

There is, and it's called closures. Not much shorter, but certainly more readable.

def __init__(self, *args, **kwargs):
    setattr(self, 'clean_%s' % your_field_name, self.closure_clean(your_field_name))

def closure_clean(self, field_name):
    def generic_clean():
        # your dynamic clean logic here...
        return self.cleaned_data.get(field_name)
    return generic_clean

#

Please login first before commenting.