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

Comments

Please login first before commenting.