Can't add field to ModelForm at __init__
我对ModelForm有问题。显示字段"test1",但不显示"test2"。打垒球场没有帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # models.py class Country(models.Model): name = CharField(max_length=100) # admin.py class CountryAdminForm(ModelForm): test1 = forms.CharField('test1') def __init__(self, *args, **kwargs): super(CountryAdminForm, self).__init__(*args, **kwargs) self.fields['test2'] = forms.CharField('test2') class Meta: model = Country class CountryAdmin(admin.ModelAdmin): form = CountryAdminForm admin.site.register(Country, CountryAdmin) |
谢谢您。
解决了的!
1 2 3 4 5 6 7 8 9 10 11 | class CountryAdminForm(ModelForm): test1 = forms.CharField('test1') def __init__(self, *args, **kwargs): super(CountryAdminForm, self).__init__(*args, **kwargs) self.fields['test2'] = forms.CharField('test2') self.Meta.fields.append('test2') class Meta: model = Country fields = ['name', 'test1',] |
…
我不能在django 1.4中使用self.meta.fields.append,因为所讨论的字段是一个元组。例如,您必须与self.meta.fields+=(‘另一个_field’,)连接。