Adding a view permission to Django models

A common request in Django is to use Django's permission model to control who can view particular object types.  This is pretty natural, especially as Django already provides default permissions on all models to 'create', 'modify' and 'delete'.

There are various answers to that question on the web, but none of them seemed particularly elegant.  After some more hunting around, I found that adding the view permission was most naturally done in a post_syncdb hook.  Whenever you issue a 'syncdb' command, all content types can be checked to see if they have a 'view' permission, and if not, create one.

Just grab this gist and throw it in your __init__.py in a management/ directory of one of your apps.  It needs to live under a management directory or it won't be discovered with the syncdb hook.

Once there, it should automatically create a view permission for all your objects upon your next syncdb.

9 responses
I've given some user's to Add permission on specific models. And I want to show them objects added by them. Is it possible? Thanks
Hi, I used the snippet and it worked. The permisision "to view" appears but never happens with the user view. Do you know how to fix this?
All I get upon logging in with a user that has "view" permission is "You don't have permission to edit anything."
Hola a todos, muy bueno!!!, para unir con el admin pense en extender el mismo de la siguiente forma y luego utilizarlo (Utilice Django 1.6): from django.contrib import admin from polls.models import Choice, Poll from django.contrib.contenttypes.models import ContentType class MyAdmin(admin.ModelAdmin): def has_change_permission(self, request, obj=None): ct = ContentType.objects.get_for_model(self.model) salida = False if request.user.is_superuser: salida = True else: if request.user.has_perm('%s.view_%s' % (ct.app_label, ct.model)): salida = True else: if request.user.has_perm('%s.change_%s' % (ct.app_label, ct.model)): salida = True else: salida = False return salida def get_readonly_fields(self, request, obj=None): ct = ContentType.objects.get_for_model(self.model) if not request.user.is_superuser and request.user.has_perm('%s.view_%s' % (ct.app_label, ct.model)): return [el.name for el in self.model._meta.fields] class MyAdminTabularInline(admin.TabularInline): def has_change_permission(self, request, obj=None): ct = ContentType.objects.get_for_model(self.model) salida = False if request.user.is_superuser: salida = True else: if request.user.has_perm('%s.view_%s' % (ct.app_label, ct.model)): salida = True else: if request.user.has_perm('%s.change_%s' % (ct.app_label, ct.model)): salida = True else: salida = False return salida def get_readonly_fields(self, request, obj=None): ct = ContentType.objects.get_for_model(self.model) if not request.user.is_superuser and request.user.has_perm('%s.view_%s' % (ct.app_label, ct.model)): return [el.name for el in self.model._meta.fields] class ChoiceInline(MyAdminTabularInline): model = Choice extra = 3 class PollAdmin(MyAdmin): list_display = ('question', 'pub_date', 'was_published_recently') list_filter = ['pub_date'] search_fields = ['question'] fieldsets = [ (None, {'fields': ['question']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [ChoiceInline] admin.site.register(Poll, PollAdmin) Donde MyAdmin y MyAdminTabularInline podrian ser parte de una libreria y luego reutilizarlos. Saludos Espero que les sirva
5 visitors upvoted this post.