Create django groups programmatically
Asked Answered
G

2

10

I want to create groups in django programmatically, but not in a view, but rather in something like model (for example using migrations). How to do it? There's no information about it in google and docs (at least not here: https://docs.djangoproject.com/en/1.7/topics/auth/default/#groups)

Goon answered 3/8, 2014 at 14:10 Comment(1)
Okay, you need to clarify what you mean by "migrations". And I really, really hope that you mean you're using South and you're writing a data migration.Lareine
L
13

Okay, it seems you're using Django 1.7's new migrations system. This is similar to but not exactly like South.

A migration that involves altering the data in the tables is a data migration, and you typically need to write Python code to do the migration.

From the Django docs, there's this example:

# -*- coding: utf-8 -*-
from django.db import models, migrations

def combine_names(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Person = apps.get_model("yourappname", "Person")
    for person in Person.objects.all():
        person.name = "%s %s" % (person.first_name, person.last_name)
        person.save()

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(combine_names),
    ]

Note that the code to run during the migration is in the combine_names function, which is called by the migrations.RunPython(combine_names) entry in the operations list of the migration. Your migration should do its group creation in a function like that, along with whatever other data migration is needed.

You should probably use a line like

Group = apps.get_model("auth", "Group")
my_group, created = Group.objects.get_or_create(name='group1')

to create your groups, in case there is already a group of that name in the table.

Don't put code to run during a migration into the root level of the Python file; if you do so, it will be run every time that migration is imported, for example, every time you run ./manage.py runserver.

P.S. You need to put your migrations.RunPython entry at the right point in the operations list; it won't work if you put it after an operation that deletes a table it needs, for example.

Lareine answered 3/8, 2014 at 15:10 Comment(1)
I'm accepting this answer because it's more expanded and because of reminding me to check isn't there already a group defined, thanksGoon
A
6

Groups are just like any other Django model. You can create them as you would anything else.

my_group = Group.objects.create(name='group1')
Alternant answered 3/8, 2014 at 14:46 Comment(9)
I know it... But this code works only in view isn't it? If I'm wrong please tell where I should put it.Goon
I don't understand your question. You can run this code anywhere you like: in a migration, in the shell, in a custom management command...Alternant
I want to execute this only once (like models are created once). When I'm wrtiting it in migrations, it runs everytime I launch runserver.Goon
It works anywhere Django does. Views, management commands, triggers, migrations, whatever. Shoot, if you have a ForeignKey(Group) in the admin, there's a green + next to the popup menu that lets you create a new group on the spot.Lareine
@Goon what? Each migration only runs once, that's the whole point.Alternant
I made makemigrations command, and at the bottom of the generated file I put this code as above. Now every time I'm trying to runserver I'm getting django.db.utils.IntegrityError: column name is not unique.Goon
@DanielRoseman: Sounds like b put the code at the root level of the migration script, so it runs on import.Lareine
@MikeDeSimone, yes, so where's correct place? All what I'm trying causes this error.Goon
Since you mention makemigrations you are evidently using 1.7 with the new built-in migration functionality. The documentation is very clear about where to put code for a data migration.Alternant

© 2022 - 2024 — McMap. All rights reserved.