Solved.
In __init__.py
...
from django.db.models.signals import post_syncdb
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def add_view_permissions(sender, **kwargs):
"""
This syncdb hooks takes care of adding a view permission too all our
content types.
"""
# for each of our content types
for content_type in ContentType.objects.all():
# build our permission slug
codename = "view_%s" % content_type.model
# if it doesn't exist..
if not Permission.objects.filter(content_type=content_type,
codename=codename):
# add it
Permission.objects.create(content_type=content_type,
codename=codename,
name="Can view %s" %
content_type.name)
print("Added view permission for %s" % content_type.name)
# check for all our view permissions after a syncdb
post_syncdb.connect(add_view_permissions)
...
In admin.py
...
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]
return self.readonly_fields
...
in models.py
...
class City(models.Model):
nome_cidade = models.CharField(max_length=100)
estado_cidade = models.CharField(max_length=100)
pais_cidade = models.CharField(max_length=100)
def __str__(self):
return self.nome_cidade
class Meta:
permissions = (
('view_city', 'Can view city'),
)
...
Thank's for help!
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b7bda756-e5ed-4f70-8524-46daec061e23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.