'Validate pydantic field against a list of values
I would like to query the Meals database table to obtain a list of meals (i.e. 1= breakfast, 2= lunch, 3= dinner, etc.), and validate the Recipe meal_id contains one of these values. Is this possible? Any suggestions how?
# Models
class Meal(Base):
__tablename__ = "meals"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True, unique=True)
class Recipe(Base):
__tablename__ = "recipes"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
note = Column(String, nullable=True)
star = Column(Boolean, default=False)
meal_id = Column(Integer, ForeignKey("meals.id"), nullable=True)
details = relationship("RecipeIngredient")
owner_id = Column(Integer, ForeignKey("users.id"))
# Schemas
class Meal(BaseModel):
name: str
class Recipe(BaseModel):
name: str
note: Optional[str] = None
star: Optional[bool] = False
meal_id : Optional[str] = None
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
