• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

related_name 오류

23.11.07 15:17 작성 조회수 125

0

안녕하세요 강사님,, 또 오류가 나서 질문 드립니다 ㅜㅜ

class User(AbstractUser):
    class GenderChoices(models.TextChoices):
        MALE = "M", "남성" #전자는 DB에 저장되는 값, 후자는 실제 보여지는 값
        FEMALE = "F", "여성"

    follower_set = models.ManyToManyField(
        "self", blank=True, symmetrical=False,
        related_name="following_set",
    )
    following_set = models.ManyToManyField(
        "self", blank=True, symmetrical=False,
        related_name="follower_set",
    )

이렇게 단방향으로 맞춰주었는데,

저장을 하면

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
accounts.User.follower_set: (fields.E302) Reverse accessor for 'accounts.User.follower_set' clashes with field name 'accounts.User.following_set'.    
        HINT: Rename field 'accounts.User.following_set', or add/change a related_name argument to the definition for field 'accounts.User.follower_set'.
accounts.User.follower_set: (fields.E303) Reverse query name for 'accounts.User.follower_set' clashes with field name 'accounts.User.following_set'.  
        HINT: Rename field 'accounts.User.following_set', or add/change a related_name argument to the definition for field 'accounts.User.follower_set'.
accounts.User.following_set: (fields.E302) Reverse accessor for 'accounts.User.following_set' clashes with field name 'accounts.User.follower_set'.   
        HINT: Rename field 'accounts.User.follower_set', or add/change a related_name argument to the definition for field 'accounts.User.following_set'.
accounts.User.following_set: (fields.E303) Reverse query name for 'accounts.User.following_set' clashes with field name 'accounts.User.follower_set'. 
        HINT: Rename field 'accounts.User.follower_set', or add/change a related_name argument to the definition for field 'accounts.User.following_set'.

계속 이렇게 오류가 뜹니다 ㅜㅜ 왜이럴까요..허허 ㅜㅜ

답변 1

답변을 작성해보세요.

0

안녕하세요.

강의에서는 related_name을 지정하지 않았었는 데, 아래와 같이 related_name을 지정해주신 부분이 다른 점이구요. follower_set의 related_name으로 지정하신 following_set 이름과 그 다음 M2M 필드인 following_set 이름이 충돌이 나서 발생하는 이슈입니다.

image

강의처럼 related_name을 빼시어 처리하셔도 되고, related_name을 지정하실려면 M2M 필드는 둘 중에 하나만 지정하셔도 됩니다.

아래처럼 지정하셔도 동작을 합니다.

image

질문에 감사드립니다.

살펴보시고 댓글 남겨주세요.

화이팅입니다. :-)