Django管理员使用forms.py覆盖字段值

时间:2014-01-27 11:20:22

标签: python django

我正在尝试这个:

在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正在工作)

0 个答案:

没有答案