1

I have some fields that do not need to be displayed to the user, but need to be editable by the administrator in the admin panel. The solution provided by Manoj Govindan here only displays the field in admin, but after saving and returning to the form in admin, the fields are unset again. I am using 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)

and 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)
Community
  • 1
  • 1
Euphorbium
  • 1,177
  • 5
  • 17
  • 35
  • 2
    So you want the field to be in the app and editable, but you don't want the user to see it? If so you are going about it wrong. They should not be set to `editable=False` if you need to be able to edit them... You should use another method such as a custom form that does not contain the field to show the user. – Ngenator Oct 07 '14 at 13:13
  • Why was it possible to do this on django 1.2.3? Why does my way not throw any exceptions then? – Euphorbium Oct 07 '14 at 13:30
  • 1
    There's nothing wrong with the code, you're just trying to set something to not be editable, and then trying to edit it by forcing it to appear on the admin page. As for the versions, at i think 1.5 maybe, they made forms more strict, you have to specify what fields you want to show in a form for security reasons so that the users only see what they're supposed to see. – Ngenator Oct 07 '14 at 13:34

0 Answers0