如何在django admin中编辑editable=False字段?

7 浏览
0 Comments

如何在django admin中编辑editable=False字段?

我有一些字段不需要显示给用户,但需要在管理员面板中可编辑。Manoj Govindan在这里提供的解决方案只在管理员界面中显示字段,但在保存并返回到管理员界面的表单后,字段又被重置了。我使用的是django 1.6.5版本。

我的models.py文件如下:

class Profile(models.Model):
    user = models.OneToOneField(User)
    picture1 = ImageWithThumbsField(_("照片1"), upload_to="photos", null=True,
        blank=True, sizes=((150, 225),))
    picture1_confirm = models.NullBooleanField(null=True, editable=False)
    picture2 = ImageWithThumbsField(_("照片2"), upload_to="photos", null=True,
        blank=True, sizes=((150, 225),))
    picture2_confirm = models.NullBooleanField(null=True, editable=False)
    picture3 = ImageWithThumbsField(_("照片3"), upload_to="photos", null=True,
        blank=True, sizes=((150, 225),))
    picture3_confirm = models.NullBooleanField(null=True, editable=False)

而admin.py文件如下:

class CustomProfileForm(forms.ModelForm):
    picture1_confirm = forms.BooleanField(required=False)
    picture2_confirm = forms.BooleanField(required=False)
    picture3_confirm = forms.BooleanField(required=False)
    class Meta:
        model = Profile
        fields = ('picture1', 'picture1_confirm', 'picture2', 'picture2_confirm',
            'picture3', 'picture3_confirm',)
class ProfileAdmin(admin.ModelAdmin):
    form = CustomProfileForm
admin.site.register(Profile, ProfileAdmin)

0