我有使用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)
谢谢。
答案 0 :(得分:9)
解决!
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',]
...
答案 1 :(得分:1)
我无法在Django 1.4中使用 self.Meta.fields.append ,因为有问题的字段是一个元组。例如,您必须与 self.Meta.fields + =('another_field',)连接。