Login

newforms field callback helper

Author:
SmileyChris
Posted:
April 29, 2007
Language:
Python
Version:
.96
Tags:
newforms
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. Natural language date/time form fields by jdriscoll 7 years, 9 months ago
  2. Updated FileField / ImageField with a delete checkbox by tomZ 6 years, 12 months ago
  3. ingore_fields by RealNitro 7 years, 4 months ago
  4. Convert an instance to a dictionary for use in newforms by SmileyChris 7 years, 10 months ago
  5. update_or_create by abhin4v 6 years, 4 months ago

Comments

Please login first before commenting.