无法将字段添加到ModelForm处
我有一个ModelForm的问题。 显示字段“test1”,但“test2” - 不是。 玩base_fields没有帮助。
# 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)
谢谢。
解决了!
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 + =('another_field',)连接 。
链接地址: http://www.djcxy.com/p/54009.html上一篇: Can't add field to ModelForm at
下一篇: How can I pass a User model into a form field (django)?