Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Created October 19, 2018 12:42
Show Gist options
  • Save edgabaldi/197df9c06ac48e3146382f69681d903c to your computer and use it in GitHub Desktop.
Save edgabaldi/197df9c06ac48e3146382f69681d903c to your computer and use it in GitHub Desktop.
Cria Permissão para model proxy no django 1.6
from __future__ import unicode_literals, absolute_import, division
import sys
from django.db.models import get_models
from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentTyped
from django.utils.encoding import smart_text
for model in get_models():
opts = model._meta
ctype, created = ContentType.objects.get_or_create(
app_label=opts.app_label,
model=opts.object_name.lower(),
defaults={'name': smart_text(opts.verbose_name_raw)})
for codename, name in _get_all_permissions(opts, ctype):
p, created = Permission.objects.get_or_create(
codename=codename,
content_type=ctype,
defaults={'name': name})
if created:
sys.stdout.write('Adding permission {}\n'.format(p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment