[Answer]-Django 'related_name' validation error with User

1đź‘Ť

âś…

Firstly: You’ve written “related_name” (“who_following” field) keyword with double underscore. It’s wrong!


As long as you is using “django.contrib.auth” you should refer to User model like this:

class Follow(models.Model):
    follower = models.ForeignKey('auth.User',
        related_name='id_of_the_follower')
    who_following = models.ForeignKey('auth.User',
        related_name='id_of_the_person_being_followed')

or you can refer to model User explicitly:

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

class Follow(models.Model):
    follower = models.ForeignKey(User,
        related_name='id_of_the_follower')
    who_following = models.ForeignKey(User,
        related_name='id_of_the_person_being_followed')

However, if you want to reference the “user” model from other app in your project, you should do something like:

class Follow(models.Model):
    follower = models.ForeignKey("yourappname.User",
        related_name='id_of_the_follower')
    who_following = models.ForeignKey("yourappname.User",
        related_name='id_of_the_person_being_followed')
👤felipsmartins

Leave a comment