Multiple roles Django 2

1,035 views
Skip to first unread message

Shazia Nusrat

unread,
Jul 6, 2019, 7:07:37 PM7/6/19
to django...@googlegroups.com
Hi All, 
I am having hard time to get multiple roles for users in a project and I am having errors. 
I've added related_name but still getting error. Can please tell me how to fix this?

*******************************************
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

class Role(models.Model):
 STUDENT = 1
 TEACHER = 2
 SECRETARY = 3
 SUPERVISOR = 4
 ADMIN = 5
 ROLE_CHOICES = (
  (STUDENT, 'student'),
  (TEACHER, 'teacher'),
  (SECRETARY, 'secretary'),
  (SUPERVISOR, 'supervisor'),
  (ADMIN, 'admin'),
 )
 id = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, primary_key=True)
 def __str__(self):
  return self.get_id_display()
class User(AbstractUser):
 roles = models.ManyToManyField(Role)
*******************************************************

I am getting error below:

*******************************************************
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
users.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
users.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.

***********************************************************

Looking forward for your kind help. 

Thanks,

Shazia

Mike Dewhirst

unread,
Jul 6, 2019, 11:34:48 PM7/6/19
to django...@googlegroups.com
On 7/07/2019 9:07 am, Shazia Nusrat wrote:
> Hi All,
> I am having hard time to get multiple roles for users in a project and
> I am having errors.
> I've added related_name but still getting error. Can please tell me
> how to fix this?

The error provides a hint. You need an extra argument ...

    related_name='user_role'

... or something else which makes sense for you in the context of your
app. See ...

https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey.related_name

Mike
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD83tOyrfzvb3JCrgUFPUqTpxiaf-qLvy50nhgOLgA%2BpsjbURQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAD83tOyrfzvb3JCrgUFPUqTpxiaf-qLvy50nhgOLgA%2BpsjbURQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

laya Mahmoudi

unread,
Jul 7, 2019, 12:03:35 AM7/7/19
to django...@googlegroups.com
You should add some code in your settings.py like AUTH_USER_MODEL= 'appname.modelname' which inherits django user mode.


در تاریخ یکشنبه ۷ ژوئیهٔ ۲۰۱۹،‏ ۳:۳۷ Shazia Nusrat <shazi...@gmail.com> نوشت:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Deepraj Devikar

unread,
Jul 7, 2019, 1:00:06 PM7/7/19
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages