I have a user model which I have extended to store extra information. Now I have a department table which has two keys(foreign) which will be related to the columns in the user table. But I am getting an error.
This is what I have tried
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Profile(models.Model):
STATUS_CHOICES = (
(1, ("Permanent")),
(2, ("Temporary")),
)
GENDER_CHOICES = (
(1, ("Male")),
(2, ("Female")),
(3, ("Not Specified"))
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
emp_type = models.IntegerField(choices=STATUS_CHOICES, default=1)
contact = models.CharField(max_length=13, blank=True)
whatsapp = models.CharField(max_length=13, blank=True)
gender = models.IntegerField(choices=GENDER_CHOICES, default=3)
avatar = models.ImageField(upload_to='users/images', default='users/images/default.jpg')
manager_username = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
class Department(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=20, unique=True)
manager = models.ForeignKey(User, blank=True, null=True, related_name='manager_usernames', on_delete=models.DO_NOTHING)
tech_lead = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
Error message ERRORS:
users.Department.tech_lead: (fields.E304) Reverse accessor for 'Department.tech_lead' clashes with reverse accessor for 'Profile.manager_username'.
HINT: Add or change a related_name argument to the definition for 'Department.tech_lead' or 'Profile.manager_username'.
users.Department.tech_lead: (fields.E305) Reverse query name for 'Department.tech_lead' clashes with reverse query name for 'Profile.manager_username'.
HINT: Add or change a related_name argument to the definition for 'Department.tech_lead' or 'Profile.manager_username'.
users.Profile.manager_username: (fields.E304) Reverse accessor for 'Profile.manager_username' clashes with reverse accessor for 'Department.tech_lead'.
HINT: Add or change a related_name argument to the definition for 'Profile.manager_username' or 'Department.tech_lead'.
users.Profile.manager_username: (fields.E305) Reverse query name for 'Profile.manager_username' clashes with reverse query name for 'Department.tech_lead'.
HINT: Add or change a related_name argument to the definition for 'Profile.manager_username' or 'Department.tech_lead'.
But when I am tech_lead = models.ForeignKey(User, blank=True, null=True, related_name='usernames', on_delete=models.DO_NOTHING)
this line. Everything works fine. Can anyone tell me where am I going wrong. This problem came only when I added the departments model in my database otherwise the user model is working great.
EDIT : As pointed out in the comments I got confused between the to_field
and related_name
. I confused related_name to to_field in models.
As the error says, the related_name for Profile.manager_username
clashes with the one for Department.tech_lead
. Change one of them so that they are not the same.
(Although I must say, your modelling is quite odd. Surely a tech lead can't be in charge of more than one department? So the relationship should be one to one. And why explicitly link a Profile to a manager, rather than to a department?)