Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: How to remove fields from the admin form for specific users?
    primarykey
    data
    text
    <p>My admin looks like this (with no exclude variable):</p> <pre><code>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) </code></pre> <p>The form:</p> <pre><code>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 </code></pre> <p>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:</p> <ul> <li><a href="https://stackoverflow.com/questions/2866896/django-admin-remove-field-if-editing-an-object/2866981#2866981">Django admin - remove field if editing an object</a></li> <li><a href="https://stackoverflow.com/questions/1466512/remove-fields-from-modelform/1914812#1914812">Remove fields from ModelForm</a></li> <li><a href="http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/" rel="nofollow noreferrer">http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/</a></li> </ul> <p>However with each one I get: <code>KeyError while rendering: Key 'added_by' not found in Form</code>. It seems I need to remove the field earlier in the form rendering process but I'm stuck on where to do this.</p> <p>So how can I exclude the <code>added_by</code> field for normal users?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload