Продублирую здесь:
signals.py
from django.db.models.signals import post_migrate
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def add_view_permissions(sender, **kwargs):
"""
Add view permissions
"""
for content_type in ContentType.objects.all():
codename = "view_%s" % content_type.model
if not Permission.objects.filter(content_type=content_type, codename=codename):
Permission.objects.create(content_type=content_type, codename=codename,
name="Can view %s" % content_type.name)
post_migrate.connect(add_view_permissions)
admin.py
from django.contrib import admin
class AdminMixin(admin.ModelAdmin):
def has_change_permission(self, request, obj=None):
change_permission = super(AdminMixin, self).has_change_permission(request, obj)
if change_permission:
self.readonly_fields = AdminMixin.readonly_fields
return True
if request.user.has_perm('{}.view_{}'.format(self.model._meta.app_label, self.model._meta.object_name)):
self.readonly_fields = self.model._meta.get_all_field_names()
return True
return False
Updated 14 April 2015, 9:06 by pyCode.