=== modified file 'django/forms/models.py'
|
|
|
163 | 163 | def __init__(self, options=None): |
164 | 164 | super(ModelFormOptions, self).__init__(options) |
165 | 165 | self.model = getattr(options, 'model', None) |
| 166 | self.formfield_kwargs = getattr(options, 'formfield_kwargs', None) |
166 | 167 | |
167 | 168 | def create_model_fields(cls, attrs): |
168 | 169 | """ |
169 | 170 | Create a list of form field instances from the option 'model'. |
170 | 171 | This is used by the ModelForm metaclass. |
171 | 172 | """ |
172 | | formfield_callback = attrs.pop('formfield_callback', lambda f: f.formfield()) |
| 173 | formfield_callback = attrs.pop('formfield_callback', lambda f, **kwargs: f.formfield(**kwargs)) |
173 | 174 | fields = [] |
174 | 175 | if cls._meta.model: |
| 176 | formfield_kwargs = cls._meta.formfield_kwargs or {} |
175 | 177 | for dbfield in cls._meta.model._meta.fields + cls._meta.model._meta.many_to_many: |
176 | 178 | if dbfield.editable: |
177 | | formfield = formfield_callback(dbfield) |
| 179 | formfield = formfield_callback(dbfield, **formfield_kwargs.get(dbfield.name, {})) |
178 | 180 | if formfield: |
179 | 181 | fields.append((dbfield.name, formfield)) |
180 | 182 | cls.model_fields = SortedDict(fields) |
… |
… |
|
354 | 356 | __metaclass__ = ModelFormMetaclass |
355 | 357 | |
356 | 358 | def modelform_factory(model, form=ModelForm, fields=None, exclude=None, fieldsets=None, |
357 | | inlines=None, formfield_callback=lambda f: f.formfield()): |
| 359 | inlines=None, formfield_kwargs=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): |
358 | 360 | # HACK: we should be able to construct a ModelForm without creating |
359 | 361 | # and passing in a temporary inner class |
360 | 362 | class Meta: |
… |
… |
|
364 | 366 | setattr(Meta, 'fields', fields) |
365 | 367 | setattr(Meta, 'exclude', exclude) |
366 | 368 | setattr(Meta, 'inlines', inlines) |
| 369 | setattr(Meta, 'formfield_kwargs', formfield_kwargs) |
367 | 370 | class_name = model.__name__ + 'Form' |
368 | 371 | return ModelFormMetaclass(class_name, (form,), {'Meta': Meta, |
369 | 372 | 'formfield_callback': formfield_callback}) |
… |
… |
|
468 | 471 | form.fields[self._pk_field.name] = IntegerField(required=False, widget=HiddenInput) |
469 | 472 | super(BaseModelFormSet, self).add_fields(form, index) |
470 | 473 | |
471 | | def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(), |
| 474 | def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs), |
472 | 475 | formset=BaseModelFormSet, |
473 | 476 | extra=1, can_delete=False, can_order=False, |
474 | | max_num=0, fields=None, exclude=None, fieldsets=None, inlines=None): |
| 477 | max_num=0, fields=None, exclude=None, fieldsets=None, inlines=None, formfield_kwargs=None): |
475 | 478 | """ |
476 | 479 | Returns a FormSet class for the given Django model class. |
477 | 480 | """ |
478 | 481 | form = modelform_factory(model, form=form, fields=fields, exclude=exclude, |
479 | | fieldsets=fieldsets, inlines=inlines, |
| 482 | fieldsets=fieldsets, inlines=inlines, formfield_kwargs=formfield_kwargs, |
480 | 483 | formfield_callback=formfield_callback) |
481 | 484 | FormSet = formset_factory(form, formset, extra=extra, max_num=max_num, |
482 | 485 | can_order=can_order, can_delete=can_delete) |
… |
… |
|
566 | 569 | |
567 | 570 | def inlineformset_factory(parent_model, model, form=ModelForm, |
568 | 571 | formset=BaseInlineFormSet, fk_name=None, |
569 | | fields=None, exclude=None, fieldsets=None, inlines=None, |
| 572 | fields=None, exclude=None, fieldsets=None, inlines=None, formfield_kwargs=None, |
570 | 573 | extra=3, can_order=False, can_delete=True, max_num=0, |
571 | | formfield_callback=lambda f: f.formfield()): |
| 574 | formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): |
572 | 575 | """ |
573 | 576 | Returns an ``InlineFormSet`` for the given kwargs. |
574 | 577 | |
… |
… |
|
595 | 598 | 'fields': fields, |
596 | 599 | 'exclude': exclude, |
597 | 600 | 'inlines': inlines, |
| 601 | 'formfield_kwargs': formfield_kwargs, |
598 | 602 | 'max_num': max_num, |
599 | 603 | } |
600 | 604 | FormSet = modelformset_factory(model, **kwargs) |