0

I am getting the following error:

    dsapi.Profile.user: (fields.E304) Reverse accessor for 'Profile.user' clashes with reverse accessor for 'Profile.user'.
        HINT: Add or change a related_name argument to the definition for 'Profile.user' or 'Profile.user'.
dsapi.Profile.user: (fields.E305) Reverse query name for 'Profile.user' clashes with reverse query name for 'Profile.user'.

All of my research has shown to create a related_name for each of the issues. But in my case, it's telling me that the reverse accessor has an issue with itself. Does anyone have any thoughts here? Below is the problematic code in my models.py file.

import uuid
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


class DsapiModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)

    class Meta:
        abstract = True

class Profile(DsapiModel):
    """
    Extend User model using one-to-one link
    Save a query using users = User.objects.all().select_related('profile')
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    favorite_sites = models.ManyToManyField(Site)

    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()
jonesy19
  • 139
  • 1
  • 16

1 Answers1

1

The error indicates that there are multiple Profile <-> User relation exists in your project.

Arbitrary, You can have as much as a relation between them, But, their related_name must be different.

Here is one SO post that may help you to understand the related_name

  1. What is related_name used for in Django?
JPG
  • 82,442
  • 19
  • 127
  • 206