assign fields dynamically in newforms

 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. Render dynamically assigned fields in a template by rubic 7 years, 1 month ago
  2. A Lazy ChoiceField implementation by lsbardel 4 years, 6 months ago
  3. Dynamic tabular inlines with optional drag-n-drop sorting by Aneon 4 years, 11 months ago
  4. Improved Pickled Object Field by taavi223 4 years, 8 months ago
  5. Getting dynamic model choices in newforms by ubernostrum 7 years, 1 month 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

#

(Forgotten your password?)