Login

newforms field callback helper

Author:
SmileyChris
Posted:
April 29, 2007
Language:
Python
Version:
.96
Score:
5 (after 5 ratings)

Now redundant any anything >0.96, as form_for_* methods now have a fields attribute

formfield_callbacks are a bit difficult to use, here's a helper method to create a callback function to use with the form_for_instance and form_for_model methods.

Example usage:

person_callback = new_callback(exclude=['password', 'can_add_staff', 'is_staff'])
def form_for_person(person):
    return form_for_instance(person, formfield_callback=person_callback)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def new_callback(exclude=None, include=None):
    """
    Create a form callback which excludes or only includes certain fields.
    """
    def _callback(field, **kwargs):
        if include and field.name not in include:
            return None
        if exclude and field.name in exclude:
            return None
        return field.formfield(**kwargs)
    return _callback

More like this

  1. Form field with fixed value by roam 1 week, 2 days ago
  2. New Snippet! by Antoliny0919 2 weeks, 2 days ago
  3. Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months ago
  4. get_object_or_none by azwdevops 6 months, 3 weeks ago
  5. Mask sensitive data from logger by agusmakmun 8 months, 3 weeks ago

Comments

Please login first before commenting.