Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to remove fields from the admin form for specific users?

My admin looks like this (with no exclude variable):

class MovieAdmin(models.ModelAdmin)
    fields = ('name', 'slug', 'imdb_link', 'start', 'finish', 'added_by')
    list_display = ('name', 'finish', 'added_by')
    list_filter = ('finish',)
    ordering = ('-finish',)
    prepopulated_fields = {'slug': ('name',)}

    form = MovieAdminForm

    def get_form(self, request, obj=None, **kwargs):
        form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
        form.current_user = request.user
        return form

admin.site.register(Movie, MovieAdmin)

The form:

class MovieAdminForm(forms.ModelForm):

    class Meta:
        model = Movie

    def save(self, commit=False):
        instance = super(MovieAdminForm, self).save(commit=commit)
        if not instance.pk and not self.current_user.is_superuser:
            if not self.current_user.profile.is_manager:
                instance.added_by = self.current_user.profile
        instance.save()
        return instance

I'm trying to remove the added_by field for users since I'd prefer to populate that from the session. I've tried methods from the following:

  • Django admin - remove field if editing an object
  • Remove fields from ModelForm
  • http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/

However with each one I get: KeyError while rendering: Key 'added_by' not found in Form. It seems I need to remove the field earlier in the form rendering process but I'm stuck on where to do this.

So how can I exclude the added_by field for normal users?

like image 254
ghickman Avatar asked Jun 07 '11 20:06

ghickman


People also ask

How do I hide a field in Django admin panel?

For the admin list-view of the items, it suffices to simply leave out fields not required: e.g. In Django 1.8 exclude = ('fieldname',) does works with admin. ModelAdmin so one does not have to inherit from InlineModelAdmin anymore. Also works in Django 1.6.

How do you make a field non mandatory in Django admin?

The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank).

How do you make a field non editable in Django admin?

editable=False will make the field disappear from all forms including admin and ModelForm i.e., it can not be edited using any form.


1 Answers

You're probably getting that error when list_display is evaluated. You can't show a field that's excluded. The version with added_by removed also needs a corresponding list_display.

def get_form(self, request, obj=None, **kwargs):
    current_user = request.user
    if not current_user.profile.is_manager:
        self.exclude = ('added_by',)
        self.list_display = ('name', 'finish')
    form = super(MovieAdmin, self).get_form(request, obj, **kwargs)
    form.current_user = current_user
    return form
like image 81
Danny W. Adair Avatar answered Nov 02 '22 22:11

Danny W. Adair