Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save volgoweb/65c5e06dd381e132a5d0c840a00fe613 to your computer and use it in GitHub Desktop.
Save volgoweb/65c5e06dd381e132a5d0c840a00fe613 to your computer and use it in GitHub Desktop.
# Generated by Django 2.2 on 2019-06-06 14:38
"""
If you add a migration that uses content types and/or permissions you should apply the functions to fill particular tables in DB.
"""
from django.db import migrations
from django.contrib.contenttypes.management import create_contenttypes
from django.contrib.auth.management import create_permissions
from django.apps import apps as _apps
def add_team_owner_group(apps, schema_editor):
for app_config in _apps.get_app_configs():
# Fill the table of rows with every model.
create_contenttypes(app_config)
# Fill the table of rows with every permission.
create_permissions(app_config)
Group = apps.get_model('auth', 'Group')
g = Group.objects.create(
name='SOME_GROUP',
)
Permission = apps.get_model('auth', 'Permission')
ContentType = apps.get_model('contenttypes', 'ContentType')
ct = ContentType.objects.get(app_label='cabinet', model='permissions')
perm = Permission.objects.get(
codename='SOME-PARM-CODENAME',
content_type=ct,
)
g.permissions.add(perm)
g.save()
class Migration(migrations.Migration):
dependencies = [
('contenttypes', '0001_initial'),
('auth', '0001_initial'),
('cabinet', '0001_initial'),
('teams', '0013_team'),
]
operations = [
migrations.RunPython(add_team_owner_group),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment