开发者

Why can't field be added to ModelForm in __init__() in Django 1.3

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')

    cl开发者_JAVA技巧ass 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜