Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

right way to create a django data migration that creates a group?

I would like to create data migrations that create Permissions and Groups, so that my other developers can just run the migrations and get everything set up. I was able to create the migrations and run them just fine, but now I'm getting an error when running my tests.

But if I do this:

from django.contrib.auth.models import Group

def add_operations_group(apps, schema_editor):
    Group.objects.get_or_create(name='operations')

I get:

django.db.utils.OperationalError: no such table: auth_group

If I do this:

def add_operations_group(apps, schema_editor):
    Group = apps.get_model("django.contrib.auth", "group")
    Group.objects.get_or_create(name='operations')

I get:

LookupError: No installed app with label 'django.contrib.auth'

Is there a way to do this? Or is there a "Django Way" to make sure things like permissions and groups are created?

like image 797
Chris Curvey Avatar asked Nov 02 '15 19:11

Chris Curvey


People also ask

Which command would you use to apply a migration in Django?

The Commands There are several commands which you will use to interact with migrations and Django's handling of database schema: migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

How do I recreate migration in Django?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .


2 Answers

This is how I do it:

from django.db import models, migrations


def apply_migration(apps, schema_editor):
    Group = apps.get_model('auth', 'Group')
    Group.objects.bulk_create([
        Group(name=u'group1'),
        Group(name=u'group2'),
        Group(name=u'group3'),
    ])


def revert_migration(apps, schema_editor):
    Group = apps.get_model('auth', 'Group')
    Group.objects.filter(
        name__in=[
            u'group1',
            u'group2',
            u'group3',
        ]
    ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('someapp', 'XXXX_some_migration'),
    ]

    operations = [
        migrations.RunPython(apply_migration, revert_migration)
    ]

Although, there must be a more Djangonic way.

like image 68
César Avatar answered Oct 06 '22 16:10

César


Answer from César is correct. To make it more Django create the migration file automatically by going to your django app root folder and entering:

python manage.py makemigrations <yourappname> --empty

Note: You may need python3 instead of python depending on your system configuration.

This creates an empty migration file in a sub directory of your app called 0001_initial.py

You can then alter it as per César instructions. Which worked correctly with Django 2.2

like image 43
Jonathan Homer Avatar answered Oct 06 '22 15:10

Jonathan Homer