logo

pydantic circular references

* 관련 기술 스택이 없습니다
emoji

• 서로 다른 파일에서 클래스를 가져와 타입 힌팅을 해야하는 상황에서 `pydantic.errors.ConfigError: field "" not yet prepared so type is still a ForwardRef, you might need to call Model.update_forward_refs().` 에러가 발생했다.
• 에러 메시지에서 알려주는 대로 update_forward_refs를 호출해야 했지만, import를 먼저 하게 되면서 update_forward_refs 메서드를 호출해도 해결되지 않았다.
• 이를 해결하기 위해 타입 힌팅을 문자열로 처리하고, 순환 참조되는 클래스 import를 하단에 하고 update_forward_refs를 호출하였다. 이렇게 하면 실제 모델이 생성될 때 타입 힌트에 있는 모델을 참조하지 않아 순환 참조를 해결할 수 있다.
• update_forward_refs의 역할은 문자열로 선참조된 것을 다시 클래스를 참조하도록 해주는 것으로, 모델 생성 시 순환 참조를 일으키는 클래스를 문자열로 처리하고 생성된 후에 제대로된 클래스를 가져오는 방식으로 작동한다.

thumbnail
북마크
공유하기
신고하기
1분 분량
조회수 323
profile-image아이들미연
2년 전
Copyright © 2025. Codenary All Rights Reserved.