无法在 __init__ 处将字段添加到 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)
谢谢。
I have a problem with ModelForm
. Field test1
is displayed, but test2
- is not. Playing with base_fields
didn't help.
# 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)
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了!
...
Solved!
...
我无法在 Django 1.4 中使用 self.Meta.fields.append ,因为所讨论的 fields 是一个元组。例如,您必须使用 self.Meta.fields += ('another_field',) 连接。
I couldn't use self.Meta.fields.append in Django 1.4, because the fields in question is a tuple. You have to concatenate with self.Meta.fields += ('another_field',) instead, for example.