from pydantic import BaseModel, Field from typing import Optional, List, ForwardRef # Shared properties class CategoryBase(BaseModel): name: str = Field(..., description="Category name", example="Electronics") description: Optional[str] = Field(None, description="Category description", example="Electronic devices and accessories") # Properties to receive via API on creation class CategoryCreate(CategoryBase): pass # Properties to receive via API on update class CategoryUpdate(BaseModel): name: Optional[str] = Field(None, description="Category name", example="Electronics") description: Optional[str] = Field(None, description="Category description", example="Electronic devices and accessories") # Properties shared by models stored in DB class CategoryInDBBase(CategoryBase): id: int = Field(..., description="The unique identifier of the category") class Config: from_attributes = True # Properties to return to client class Category(CategoryInDBBase): pass # Forward reference for Item Item = ForwardRef("Item") # Properties to return to client with relationships class CategoryWithItems(CategoryInDBBase): items: List[Item] = Field([], description="Items in this category") # Import here to avoid circular import (moved down to fix linting) from app.schemas.item import Item # noqa: E402 CategoryWithItems.model_rebuild() # Update forward references