Search code examples
pythondjangodjango-rest-frameworkrbac

How to create users and groups migrations in Django?


So I've got an API built in Django with Django Rest Framework and I now want to add a role based access control to it. For this I found the django-rest-framework-roles extension. I've got it installed, but I'm not really familiar with the usual authentication system in Django. It says I need to define the groups in the settings as

ROLE_GROUPS = [group.name.lower() for group in Group.objects.all()]

So I need the Group model and of course also the User model. As far as I understand, these are standard models. However, I don't have any tables in my DB for them. So I need a migration for it, but I'm unsure how I can do that.

I guess it should be really easy, but even on the relevant pages in the documentation I don't see any mention on how to add these models to my Django installation.

What can I try next?


Solution

  • In your settings.py you have something like this:

    INSTALLED_APPS = [
        ...
        "django.contrib.auth",
        ...
    ]
    

    That app has the Group and User models(included django app), so the first thing that you will do after config the database is migrate with this command./manage.py migrate, after migrate you can use importing them like this: from django.contrib.auth.models import User, Group