related_name의 역할
423
작성한 질문수 8
related_name의 역할이 정확하게 무엇인가요?
# profileapp의 models.py
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
# articleapp의 models.py
writer = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='article', null=True)
# commentapp의 models.py
article = models.ForeignKey(Article, on_delete=models.SET_NULL, null=True, related_name='comment')
writer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='comment')
relaated_name이 정확하게 어떻게 쓰이는지 잘 모르겠습니다..!
답변 1
0
안녕하세요.
질문 확인했습니다.
일단 User 객체와 Profile 관계를 예로 들겠습니다.
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
연결고리 역할을 하는 Key는 Profile 모델에 작성되어있습니다.
그렇다면 profile 객체를 확보했을때, 해당 profile 에 연결되어있는 user 객체를 접근하는 것은 너무나도 자명합니다.
profile.user
과 같은 형식으로 접근하면 되죠.
그런데, 반대로 user 객체를 확보한 상태에서 이에 연결된 profile 객체를 접근할때는 어떤식으로 접근해야 할까요?
user.profile
과 같이 접근합니다.
그런데 이때 profile 이라는 이 단어가 related_name 으로 저희가 원하는대로 커스터마이징이 가능한 것입니다.
만약 related_name 을 "prof" 로 설정했다면,
user 객체에서 profile 을 접근할때
user.prof
와 같이 접근해야 하는 것이죠.
결과적으로 user 객체에서 profile 을 접근하기 위한 역참조 문자열을 설정하기 위한 옵션이라고 이해해주시면 될 것 같습니다.
답변이 도움이 되셨으면 좋겠네요!
감사합니다-
강의에 나왔던 js 파일이 깃허브에 없습니다
1
79
1
모바일 디버깅, 반응형 레이아웃 4분48초 질문
0
57
1
decorator 관련질문입니다.
0
63
1
PasswordChangeView
0
104
2
로그아웃뷰 작동 관련 (2025년 3월)
1
138
1
실행에러질문
0
159
1
@login_required 데코레이터 사용시 리다이렉트는 어디서 참조하여 설정을 하는걸까요?
0
116
1
CacheBackend 관련 에러
0
181
1
21강 CreateView를 통한 회원가입 구현 질문
0
326
1
53강 disallowedhost
0
347
2
502 Bad Gateway
0
474
0
mariadb 접근권한 오류
0
546
1
logout 후 빈 화면으로 이동합니다 ㅠㅠ
0
462
2
로그아웃 후 빈 화면으로 이동
0
334
1
서버 운영 관련 질문 드립니다.(Unable to retirve...)
0
233
1
static/base.css파일을 인식을 못합니다
0
383
1
안녕하세요 19강 디버깅 설정 질문있습니다.
0
292
1
프로필 update편 질문있습니다.
0
380
1
수업질문
0
395
1
COOP error
0
531
1
61강 Dockerfile error
0
598
1
static안에 base.css 에서 정의한 클래스가 적용되지 않습니다.
0
492
1
static 파일 중 jpg 파일만 로드 불가
0
419
1
58강 static 파일 적용 안됨
0
548
1





