Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create django groups programmatically

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)

like image 231
b174662 Avatar asked Aug 03 '14 14:08

b174662


People also ask

How do I set permission and group in Django?

With Django, you can create groups to class users and assign permissions to each group so when creating users, you can just assign the user to a group and, in turn, the user has all the permissions from that group. To create a group, you need the Group model from django. contrib. auth.

Is authenticated Django?

Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project's needs.


2 Answers

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.

like image 77
Mike DeSimone Avatar answered Oct 25 '22 13:10

Mike DeSimone


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

my_group = Group.objects.create(name='group1')
like image 25
Daniel Roseman Avatar answered Oct 25 '22 13:10

Daniel Roseman