Ticket #13769: formmodel_fields_attrs_r13353.patch

File formmodel_fields_attrs_r13353.patch, 2.2 KB (added by Rolando Espinoza La fuente, 14 years ago)
  • django/forms/models.py

    # HG changeset patch
    # Parent 1fd21f6a77e8dee636bd0edc66a4fbfbf8d35f7b
    # User Rolando Espinoza La fuente <darkrho@gmail.com>
    diff --git a/django/forms/models.py b/django/forms/models.py
    a b def model_to_dict(instance, fields=None,  
    150150            data[f.name] = f.value_from_object(instance)
    151151    return data
    152152
    153 def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
     153def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs), fields_attrs={}):
    154154    """
    155155    Returns a ``SortedDict`` containing form fields for the given model.
    156156
    def fields_for_model(model, fields=None,  
    175175            kwargs = {'widget': widgets[f.name]}
    176176        else:
    177177            kwargs = {}
     178        if fields_attrs and f.name in fields_attrs:
     179            # @@@ prevent collision with widget
     180            if 'widget' in fields_attrs[f.name]:
     181                del fields_attrs[f.name]['widget']
     182            kwargs.update(fields_attrs[f.name])
    178183        formfield = formfield_callback(f, **kwargs)
    179184        if formfield:
    180185            field_list.append((f.name, formfield))
    class ModelFormOptions(object):  
    192197    def __init__(self, options=None):
    193198        self.model = getattr(options, 'model', None)
    194199        self.fields = getattr(options, 'fields', None)
     200        self.fields_attrs = getattr(options, 'fields_attrs', None)
    195201        self.exclude = getattr(options, 'exclude', None)
    196202        self.widgets = getattr(options, 'widgets', None)
    197203
    class ModelFormMetaclass(type):  
    217223        if opts.model:
    218224            # If a model is defined, extract form fields from it.
    219225            fields = fields_for_model(opts.model, opts.fields,
    220                                       opts.exclude, opts.widgets, formfield_callback)
     226                                      opts.exclude, opts.widgets, formfield_callback,
     227                                      opts.fields_attrs)
    221228            # Override default model fields with any custom declared ones
    222229            # (plus, include all the other declared fields).
    223230            fields.update(declared_fields)
Back to Top