Ticket #13095: ticket13095_r13294.diff

File ticket13095_r13294.diff, 3.6 KB (added by devinj, 14 years ago)

Updated patch, also fixes modelform_factory and modelformset_factory. Adds tests. Removes encoding problems of previous attempt.

  • django/forms/models.py

     
    376376    __metaclass__ = ModelFormMetaclass
    377377
    378378def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
    379                        formfield_callback=lambda f: f.formfield()):
     379                       formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    380380    # Create the inner Meta class. FIXME: ideally, we should be able to
    381381    # construct a ModelForm without creating and passing in a temporary
    382382    # inner class.
     
    658658            form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=HiddenInput)
    659659        super(BaseModelFormSet, self).add_fields(form, index)
    660660
    661 def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
     661def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs),
    662662                         formset=BaseModelFormSet,
    663663                         extra=1, can_delete=False, can_order=False,
    664664                         max_num=None, fields=None, exclude=None):
     
    813813                          formset=BaseInlineFormSet, fk_name=None,
    814814                          fields=None, exclude=None,
    815815                          extra=3, can_order=False, can_delete=True, max_num=None,
    816                           formfield_callback=lambda f: f.formfield()):
     816                          formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    817817    """
    818818    Returns an ``InlineFormSet`` for the given kwargs.
    819819
  • tests/regressiontests/forms/models.py

     
    5656    def __unicode__(self):
    5757        return u'%s' % self.name
    5858
     59class ParentModel(models.Model):
     60    name = models.CharField(max_length=10)
     61
     62class ForeignKeyModel(models.Model):
     63    name = models.CharField(max_length=10)
     64    parent = models.ForeignKey(ParentModel)
     65
    5966class TestTicket12510(TestCase):
    6067    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
    6168    def setUp(self):
  • tests/regressiontests/forms/regressions.py

     
    102102>>> f.as_table()
    103103u'<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field data) This field is required.</li></ul><input type="hidden" name="data" id="id_data" /></td></tr>'
    104104
     105Ticket #13095 : modelformset_factory, modelform_factory, and formset_factory didn't seem to handle Meta options well.
     106
     107>>> from django import forms
     108>>> from regressiontests.forms import models
     109>>> class ForeignKeyModelForm(forms.ModelForm):
     110...     class Meta:
     111...         model = models.ForeignKeyModel
     112...         widgets = dict(name=forms.HiddenInput())
     113...
     114>>> _ = forms.models.modelform_factory(models.ForeignKeyModel, form=ForeignKeyModelForm)() # just need to check instantiation doesn't raise
     115>>> _ = forms.models.modelformset_factory(models.ForeignKeyModel, form=ForeignKeyModelForm)()
     116>>> _ = forms.models.inlineformset_factory(models.ParentModel, models.ForeignKeyModel, form=ForeignKeyModelForm)()
     117
    105118###################################################
    106119# Tests for XSS vulnerabilities in error messages #
    107120###################################################
Back to Top