我正在尝试这个:
在models.py
中[...]
class MyModel(models.Model):
firstField = models.CharField(max_length=200, blank=True, null=True, db_index=True)
在db(MyModel的一个等值)中,firstFied的值为SAVED
在forms.py
中[...]
class MyModelAdminForm(MyModelAdminForm):
addedField = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
super(MyModelAdminForm, self).__init__(*args, **kwargs)
self.fields['addedField'].label = "Added field"
self.fields['firstField'].initial = "FIRST"
self.fields['addedField'].initial = "ADDED"
class Meta:
model = MyModel
在admin.py中
[...]
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
fieldsets = (
(None, {
'fields': ('firstField', 'addedField')
}),
)
admin.site.register(MyModel, MyModelAdmin)
为什么之后,在admin中,表单的初始值为firstField SAVED,而不是forms.py中定义的FIRST(addedField正在工作)