-
-
Save javouhey/1874214 to your computer and use it in GitHub Desktop.
[Django 1.3] Restrict authors, in admin, to Super Users and Staff only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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