Ticket #11925: models.py.diff

File models.py.diff, 2.4 KB (added by andrew.mcmurry@…, 15 years ago)
  • django/forms/models.py

     
    142142            data[f.name] = f.value_from_object(instance)
    143143    return data
    144144
    145 def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda f: f.formfield()):
     145def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda f: f.formfield(), params={}):
    146146    """
    147147    Returns a ``SortedDict`` containing form fields for the given model.
    148148
     
    152152    ``exclude`` is an optional list of field names. If provided, the named
    153153    fields will be excluded from the returned fields, even if they are listed
    154154    in the ``fields`` argument.
     155
     156    ``params`` is an optional dict mapping field names to dicts of keyword
     157    arguments to be passed to the function that generates the form field from
     158    the model field.
    155159    """
    156160    field_list = []
    157161    opts = model._meta
     
    162166            continue
    163167        if exclude and f.name in exclude:
    164168            continue
    165         formfield = formfield_callback(f)
     169        formfield = formfield_callback(f, **params.get(f.name, {}))
    166170        if formfield:
    167171            field_list.append((f.name, formfield))
    168172    field_dict = SortedDict(field_list)
     
    175179        self.model = getattr(options, 'model', None)
    176180        self.fields = getattr(options, 'fields', None)
    177181        self.exclude = getattr(options, 'exclude', None)
     182        self.params = getattr(options, 'params', {})
    178183
    179184
    180185class ModelFormMetaclass(type):
    181186    def __new__(cls, name, bases, attrs):
    182187        formfield_callback = attrs.pop('formfield_callback',
    183                 lambda f: f.formfield())
     188                lambda f, **p: f.formfield(**p))
    184189        try:
    185190            parents = [b for b in bases if issubclass(b, ModelForm)]
    186191        except NameError:
     
    198203        if opts.model:
    199204            # If a model is defined, extract form fields from it.
    200205            fields = fields_for_model(opts.model, opts.fields,
    201                                       opts.exclude, formfield_callback)
     206                                      opts.exclude, formfield_callback,
     207                                      opts.params)
    202208            # Override default model fields with any custom declared ones
    203209            # (plus, include all the other declared fields).
    204210            fields.update(declared_fields)
Back to Top