Django Reverse accessor clash error

◾️ One Model’s two field refer same Model

-> this cause error

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

  • User model with problem
from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):

    ....

    language = models.CharField(
        max_length=20, choices=LANG_CHOICES, default=LANG_EN)
    fav_book_genre = models.ForeignKey(
        "categories.Category", on_delete=models.SET_NULL, null=True)
    fav_movie_genre = models.ForeignKey(
        "categories.Category", on_delete=models.SET_NULL, null=True)

◾️ trouble shooting ( ref site )

-> use related_name

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


class User(AbstractUser):

    ....
    language = models.CharField(
        max_length=20, choices=LANG_CHOICES, default=LANG_EN)
    fav_book_genre = models.ForeignKey(
        "categories.Category", related_name='book_users', on_delete=models.SET_NULL, null=True)
    fav_movie_genre = models.ForeignKey(
        "categories.Category", related_name='movie_users', on_delete=models.SET_NULL, null=True)

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です