Models which link to Groups (by ForeignKey)

44 views
Skip to first unread message

Benjamin SOULAS

unread,
Sep 10, 2018, 5:11:50 AM9/10/18
to Django users
Hello everyone,

I want to implement a model which make references to 2 groups, to do this, I thought I had to defined, in the model which links the 2 groups, a foreign key for each Group like this:

from django.contrib.auth.models import Group
from django.db import models


class GroupLinker(models.Model):
EMSGroup = models.ForeignKey(Group, on_delete=models.CASCADE)
LDAPGroup = models.ForeignKey(Group, on_delete=models.CASCADE)

But I got that error: 

ERRORS:
api.GroupLinker.EMSGroup: (fields.E304) Reverse accessor for 'GroupLinker.EMSGroup' clashes with reverse accessor for 'GroupLinker.LDAPGroup'.
        HINT: Add or change a related_name argument to the definition for 'GroupLinker.EMSGroup' or 'GroupLinker.LDAPGroup'.
api.GroupLinker.LDAPGroup: (fields.E304) Reverse accessor for 'GroupLinker.LDAPGroup' clashes with reverse accessor for 'GroupLinker.EMSGroup'.
        HINT: Add or change a related_name argument to the definition for 'GroupLinker.LDAPGroup' or 'GroupLinker.EMSGroup'.

I don't understand why I should add a related name, because my future table will just link my 2 groups right? Where am I wrong?

Kind regards,

Benjamin.

Jason

unread,
Sep 10, 2018, 7:29:46 AM9/10/18
to Django users
https://stackoverflow.com/questions/2642613/what-is-related-name-used-for-in-django

As you can see there, its for the relation from Group to GroupLinker.  That relation name is the same for both fields, so you need to specify something unique.

Benjamin SOULAS

unread,
Sep 10, 2018, 8:03:30 AM9/10/18
to Django users
Ow, ok, I got it, thx a lot

But in order to create this object (retrieve my groups from the ORM), should I implement something specific? Because for my request, I just want to specify the group names and then, in my app, retrieve those, create my link between the groups...

I thought I had to retrieve the groups in my GroupLinkerSerializer and, then, create my instance, but to create it (correct me if I am wrong), I should retrieve Group objects and instantiate my GroupLinkerSerializer AND create it (with .create method) right?

I am not comfortable with the regular process, my API don't use a lot the ORM (only for User, Groups and Permissions).

Kind regards,

Benjamin
Reply all
Reply to author
Forward
0 new messages