Fix circular import issue in schema files
This commit is contained in:
parent
6a5649a49c
commit
8eed6b7816
@ -1,5 +1,9 @@
|
||||
# Import schemas in order to avoid circular dependencies
|
||||
# First, import schemas without relationships
|
||||
from app.schemas.author import Author, AuthorCreate, AuthorInDB, AuthorUpdate, AuthorWithManga
|
||||
from app.schemas.genre import Genre, GenreCreate, GenreInDB, GenreUpdate
|
||||
|
||||
# Then, import schemas with relationships
|
||||
from app.schemas.manga import Manga, MangaCreate, MangaInDB, MangaUpdate, MangaWithRelations
|
||||
from app.schemas.publisher import (
|
||||
Publisher,
|
||||
|
@ -1,7 +1,11 @@
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.schemas.manga import Manga
|
||||
|
||||
|
||||
# Shared properties
|
||||
class AuthorBase(BaseModel):
|
||||
@ -41,6 +45,4 @@ class AuthorInDB(AuthorInDBBase):
|
||||
|
||||
# Additional properties for response with manga list
|
||||
class AuthorWithManga(Author):
|
||||
from app.schemas.manga import Manga # Avoid circular import
|
||||
|
||||
manga_list: list[Manga] = []
|
||||
manga_list: list["Manga"] = []
|
||||
|
9
app/schemas/base.py
Normal file
9
app/schemas/base.py
Normal file
@ -0,0 +1,9 @@
|
||||
"""
|
||||
Base schemas to avoid circular imports in Pydantic models.
|
||||
This file defines common type annotations that can be reused across schema modules.
|
||||
"""
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
# Type checking imports - these won't be evaluated at runtime
|
||||
if TYPE_CHECKING:
|
||||
pass
|
@ -1,10 +1,15 @@
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from pydantic import BaseModel, Field, validator
|
||||
|
||||
from app.schemas.author import Author
|
||||
# Use forward references for type annotations
|
||||
from app.schemas.genre import Genre
|
||||
from app.schemas.publisher import Publisher
|
||||
|
||||
# Import types for type checking only
|
||||
if TYPE_CHECKING:
|
||||
from app.schemas.author import Author
|
||||
from app.schemas.publisher import Publisher
|
||||
|
||||
|
||||
# Shared properties
|
||||
@ -75,6 +80,6 @@ class MangaInDB(MangaInDBBase):
|
||||
|
||||
# Additional properties for response with relationships
|
||||
class MangaWithRelations(Manga):
|
||||
author: Author | None = None
|
||||
publisher: Publisher | None = None
|
||||
author: Optional["Author"] = None
|
||||
publisher: Optional["Publisher"] = None
|
||||
genres: list[Genre] = []
|
||||
|
@ -1,7 +1,11 @@
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.schemas.manga import Manga
|
||||
|
||||
|
||||
# Shared properties
|
||||
class PublisherBase(BaseModel):
|
||||
@ -44,6 +48,4 @@ class PublisherInDB(PublisherInDBBase):
|
||||
|
||||
# Additional properties for response with manga list
|
||||
class PublisherWithManga(Publisher):
|
||||
from app.schemas.manga import Manga # Avoid circular import
|
||||
|
||||
manga_list: list[Manga] = []
|
||||
manga_list: list["Manga"] = []
|
||||
|
Loading…
x
Reference in New Issue
Block a user