我有一些字段不需要显示给用户,但需要管理员在管理面板中进行编辑。 Manoj Govindan here提供的解决方案仅显示admin中的字段,但在保存并返回到admin中的表单后,字段将再次取消设置。我正在使用django 1.6.5
my models.py
class Profile(models.Model):
user = models.OneToOneField(User)
picture1 = ImageWithThumbsField(_("Photo1"), upload_to="photos", null=True,
blank=True, sizes=((150, 225),))
picture1_confirm = models.NullBooleanField(null=True, editable=False)
picture2 = ImageWithThumbsField(_("Photo2"), upload_to="photos", null=True,
blank=True, sizes=((150, 225),))
picture2_confirm = models.NullBooleanField(null=True, editable=False)
picture3 = ImageWithThumbsField(_("Photo3"), 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)