#11905: modelform_factory returns a broken form when given wrong value for fields -------------------------------+-------------------------------------------- Reporter: ben | Owner: nobody Status: new | Milestone: 1.2 Component: Forms | Version: 1.1 Resolution: | Keywords: modelform_factory, modelform, fields Stage: Accepted | Has_patch: 0 Needs_docs: 0 | Needs_tests: 0 Needs_better_patch: 0 | -------------------------------+-------------------------------------------- Changes (by russellm):
* stage: Unreviewed => Accepted * milestone: => 1.2 Old description: > modelform_factory is breaking where you ask for an invalid fields option > (even if the field exists): > > Here's a doctest that demonstrates: > > >>> from django.forms.models import modelform_factory, form_for_fields > >>> from django.contrib.auth.models import User > >>> Form = modelform_factory(User, fields=['id', 'username']) > >>> Form.base_fields > {'id': None, 'username': <django.forms.fields.CharField object at > 0xd4e4ad0>} > >>> print Form() > ------------------------------------------------------------ > Traceback (most recent call last): > ... > if self.field.label is None: > AttributeError: 'NoneType' object has no attribute 'label' > > Likewise you can specify fields that don't even exist on the model: > > >>> Form = modelform_factory(User, fields=['no-field', 'username']) > >>> Form.base_fields > {'no-field': None, 'username': <django.forms.fields.CharField object at > 0xd4eec10>} > >>> print Form() > ------------------------------------------------------------ > Traceback (most recent call last): > ... > if self.field.label is None: > AttributeError: 'NoneType' object has no attribute 'label' > > This appears to be due to logic in form_for_fields (line 102 in my > checkout) in the first case, and fields_for_model (line 170) in the > second. > > I'd propose some error handling be put in place to give meaningfull > exception in either of these cases rather than return a broken form to > the user. > > I'm happy to submit a patch (I can't do it from here as I'm at work and > don't have direct access to django's svn repo) but I can do it from home. > > Ben New description: modelform_factory is breaking where you ask for an invalid fields option (even if the field exists): Here's a doctest that demonstrates: {{{ >>> from django.forms.models import modelform_factory, form_for_fields >>> from django.contrib.auth.models import User >>> Form = modelform_factory(User, fields=['id', 'username']) >>> Form.base_fields {'id': None, 'username': <django.forms.fields.CharField object at 0xd4e4ad0>} >>> print Form() ------------------------------------------------------------ Traceback (most recent call last): ... if self.field.label is None: AttributeError: 'NoneType' object has no attribute 'label' Likewise you can specify fields that don't even exist on the model: >>> Form = modelform_factory(User, fields=['no-field', 'username']) >>> Form.base_fields {'no-field': None, 'username': <django.forms.fields.CharField object at 0xd4eec10>} >>> print Form() ------------------------------------------------------------ Traceback (most recent call last): ... if self.field.label is None: AttributeError: 'NoneType' object has no attribute 'label' }}} This appears to be due to logic in form_for_fields (line 102 in my checkout) in the first case, and fields_for_model (line 170) in the second. I'd propose some error handling be put in place to give meaningfull exception in either of these cases rather than return a broken form to the user. I'm happy to submit a patch (I can't do it from here as I'm at work and don't have direct access to django's svn repo) but I can do it from home. Ben -- Ticket URL: <http://code.djangoproject.com/ticket/11905#comment:4> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.