Ticket #9385: 02-formfield-kwargs.diff

File 02-formfield-kwargs.diff, 4.2 KB (added by Petr Marhoun <petr.marhoun@…>, 16 years ago)
  • django/forms/models.py

    === modified file 'django/forms/models.py'
     
    163163    def __init__(self, options=None):
    164164        super(ModelFormOptions, self).__init__(options)
    165165        self.model = getattr(options, 'model', None)
     166        self.formfield_kwargs = getattr(options, 'formfield_kwargs', None)
    166167
    167168def create_model_fields(cls, attrs):
    168169    """
    169170    Create a list of form field instances from the option 'model'.
    170171    This is used by the ModelForm metaclass.
    171172    """
    172     formfield_callback = attrs.pop('formfield_callback', lambda f: f.formfield())
     173    formfield_callback = attrs.pop('formfield_callback', lambda f, **kwargs: f.formfield(**kwargs))
    173174    fields = []
    174175    if cls._meta.model:
     176        formfield_kwargs = cls._meta.formfield_kwargs or {}
    175177        for dbfield in cls._meta.model._meta.fields + cls._meta.model._meta.many_to_many:
    176178            if dbfield.editable:
    177                 formfield = formfield_callback(dbfield)
     179                formfield = formfield_callback(dbfield, **formfield_kwargs.get(dbfield.name, {}))
    178180                if formfield:
    179181                    fields.append((dbfield.name, formfield))
    180182    cls.model_fields = SortedDict(fields)
     
    354356    __metaclass__ = ModelFormMetaclass
    355357
    356358def 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)):
    358360    # HACK: we should be able to construct a ModelForm without creating
    359361    # and passing in a temporary inner class
    360362    class Meta:
     
    364366    setattr(Meta, 'fields', fields)
    365367    setattr(Meta, 'exclude', exclude)
    366368    setattr(Meta, 'inlines', inlines)
     369    setattr(Meta, 'formfield_kwargs', formfield_kwargs)
    367370    class_name = model.__name__ + 'Form'
    368371    return ModelFormMetaclass(class_name, (form,), {'Meta': Meta,
    369372                              'formfield_callback': formfield_callback})
     
    468471            form.fields[self._pk_field.name] = IntegerField(required=False, widget=HiddenInput)
    469472        super(BaseModelFormSet, self).add_fields(form, index)
    470473
    471 def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
     474def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs),
    472475                         formset=BaseModelFormSet,
    473476                         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):
    475478    """
    476479    Returns a FormSet class for the given Django model class.
    477480    """
    478481    form = modelform_factory(model, form=form, fields=fields, exclude=exclude,
    479                              fieldsets=fieldsets, inlines=inlines,
     482                             fieldsets=fieldsets, inlines=inlines,  formfield_kwargs=formfield_kwargs,
    480483                             formfield_callback=formfield_callback)
    481484    FormSet = formset_factory(form, formset, extra=extra, max_num=max_num,
    482485                              can_order=can_order, can_delete=can_delete)
     
    566569
    567570def inlineformset_factory(parent_model, model, form=ModelForm,
    568571                          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,
    570573                          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)):
    572575    """
    573576    Returns an ``InlineFormSet`` for the given kwargs.
    574577
     
    595598        'fields': fields,
    596599        'exclude': exclude,
    597600        'inlines': inlines,
     601        'formfield_kwargs': formfield_kwargs,
    598602        'max_num': max_num,
    599603    }
    600604    FormSet = modelformset_factory(model, **kwargs)
Back to Top