Ticket #11925: models.py.diff
File models.py.diff, 2.4 KB (added by , 15 years ago) |
---|
-
django/forms/models.py
142 142 data[f.name] = f.value_from_object(instance) 143 143 return data 144 144 145 def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda f: f.formfield() ):145 def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda f: f.formfield(), params={}): 146 146 """ 147 147 Returns a ``SortedDict`` containing form fields for the given model. 148 148 … … 152 152 ``exclude`` is an optional list of field names. If provided, the named 153 153 fields will be excluded from the returned fields, even if they are listed 154 154 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. 155 159 """ 156 160 field_list = [] 157 161 opts = model._meta … … 162 166 continue 163 167 if exclude and f.name in exclude: 164 168 continue 165 formfield = formfield_callback(f )169 formfield = formfield_callback(f, **params.get(f.name, {})) 166 170 if formfield: 167 171 field_list.append((f.name, formfield)) 168 172 field_dict = SortedDict(field_list) … … 175 179 self.model = getattr(options, 'model', None) 176 180 self.fields = getattr(options, 'fields', None) 177 181 self.exclude = getattr(options, 'exclude', None) 182 self.params = getattr(options, 'params', {}) 178 183 179 184 180 185 class ModelFormMetaclass(type): 181 186 def __new__(cls, name, bases, attrs): 182 187 formfield_callback = attrs.pop('formfield_callback', 183 lambda f : f.formfield())188 lambda f, **p: f.formfield(**p)) 184 189 try: 185 190 parents = [b for b in bases if issubclass(b, ModelForm)] 186 191 except NameError: … … 198 203 if opts.model: 199 204 # If a model is defined, extract form fields from it. 200 205 fields = fields_for_model(opts.model, opts.fields, 201 opts.exclude, formfield_callback) 206 opts.exclude, formfield_callback, 207 opts.params) 202 208 # Override default model fields with any custom declared ones 203 209 # (plus, include all the other declared fields). 204 210 fields.update(declared_fields)