Mojtaba Kamyabi
Mojtaba Kamyabi

Reputation: 3610

how to add field to `auth_user_groups` table in django?

In my Django project I have users that every user may have different permission (some one has only read permission but others may be both read and write permissions) I want to add permissions field to django auth_user_groups table like this:

enter image description here here is my models.py:

from django.db.models import *
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save


class MyUser(Model):
    user = OneToOneField(User, primary_key=True)
    Volume = IntegerField(default=5*1024*1024)


def create_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = MyUser.objects.get_or_create(user=instance)

post_save.connect(create_profile, sender=User, dispatch_uid="create_profile")

is it possible to do that ?

Upvotes: 1

Views: 3376

Answers (2)

Wtower
Wtower

Reputation: 19912

I would recommend you to create an additional table with an one-to-one relationship to auth_user_groups to solve your issue rather than fiddling with that. This is a similar approach as the one suggested in the docs. Alternatively you would have to do a lot of work in order to provide your own models.

EDIT: Following up on the first comment: It is not possible to expose this model as the respective many-to-many field of the User model does not use an explicit intermediate model (in fact the many-to-many field belongs to PermissionsMixin, from which AbstractUser is derived, from which User is derived).

An one-to-one field is essentially a many-to-one foreign key with unique=True. Therefore, you could instead create two foreign keys, one for User and one for Group with unique_together and then use these.

Upvotes: 1

Geo Jacob
Geo Jacob

Reputation: 6009

Django provides permission to to group of users and individual users, You can create multiple groups and assign permissions to those groups. And assign groups to each user. So user withina a particular group will have same set of permissions.

Refer here for more https://docs.djangoproject.com/en/1.8/topics/auth/default/#permissions-and-authorization

Upvotes: 0

Related Questions