더북(TheBook)

이번에는 User 모델에서 Calendar를 향한 역방향 관계를 정의합니다. calendar가 아닌 account 앱의 models.py 파일을 수정합니다.

파이썬(/appserver/apps/account/models.py)

# 생략 
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from appserver.apps.calendar.models import Calendar  # ①

class User(SQLModel, table=True):
    __tablename__ = "users"
    # 생략
    oauth_accounts: list["OAuthAccount"] = Relationship(back_populates="user")
    calendar: "Calendar" = Relationship(  # ②
        back_populates="host",
        sa_relationship_kwargs={"uselist": False, "single_parent": True}, # ③
    )

sa_relationship_kwargs={"single_parent": True} 인자가 Relationship에 추가된 것(③) 외에는 User 모델이 OAuthAccount 모델을 역으로 가리키는 것과 Relationship이 동일합니다(②).

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.