Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created February 1, 2012 21:54
Show Gist options
  • Save mhulse/1719713 to your computer and use it in GitHub Desktop.
Save mhulse/1719713 to your computer and use it in GitHub Desktop.
[Django 1.3] Restrict authors, in admin, to Super Users and Staff only.
from django.contrib.auth.models import User
from django.contrib import admin
from dox.models import Page
#--------------------------------------------------------------------------
#
# Admin models:
#
#--------------------------------------------------------------------------
class PageAdmin(admin.ModelAdmin):
#----------------------------------
# Fields:
#----------------------------------
fields = ('author',)
#----------------------------------
# Methods:
#----------------------------------
def formfield_for_foreignkey(self, db_field, request, **kwargs):
"""
Restrict the list of authors to Super Users and Staff only.
"""
if db_field.name == 'author':
kwargs['queryset'] = User.objects.filter(is_staff=True, is_superuser=True)
return super(PageAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
#--------------------------------------------------------------------------
#
# Registrations:
#
#--------------------------------------------------------------------------
admin.site.register(Page, PageAdmin)
from django.db import models
from django.contrib.auth.models import User
class Page(models.Model):
author = models.ForeignKey(User, blank=True, null=True, help_text=_(u'Optional (used for organizational purposes only).'),)
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment