2

I have a ModelForm with a dynamically added field. It stopped working when I switched from Django 1.2 to Django 1.3. The following code is a minimal version which recreates the issue.

class MyModel(models.Model):
    rank = models.IntegerField()

    def __unicode__(self):
        return "{}".format(self.rank)


class MyModelAdminForm(forms.ModelForm):
    #dummy = forms.NullBooleanField()

    def __init__(self, *args, **kwargs):
        super(MyModelAdminForm, self).__init__(*args, **kwargs)
        self.fields['dummy'] = forms.BooleanField()
        self.fields['dummy'].required = False
        self.Meta.fields.append('dummy')

    class Meta:
        fields = ['rank']

The 'dummy' declaration which is commented out isn't needed when running under Django 1.2, everything can be done dynamically in init(). In Django 1.3 the same code gives an error:

FieldError: Unknown field(s) (dummy) specified for MyModel

Which can be fixed by adding the commented-out declaration.

I couldn't find anything in the docs that suggest a backwards-incompatible change that would cause this. Anyone know what's going on here?

JivanAmara
  • 1,065
  • 2
  • 10
  • 20
  • 2
    I remember using such a feature on django 1.3. Have you tried moving the `super(MyModelAdminForm, self).__init__(*args, **kwargs)` call after the field declaration? – Thibault J Sep 20 '11 at 08:13
  • 1
    Why do you need to add it to Meta.fields? That's only used when creating the form class. Just adding it to `self.fields` is enough. – Daniel Roseman Sep 20 '11 at 09:03

0 Answers0