-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
54 lines (37 loc) · 1.39 KB
/
models.py
File metadata and controls
54 lines (37 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from enum import Enum
from pydantic import BaseModel,validator
from sqlmodel import Field,Relationship,SQLModel
class ProductCategory(str,Enum):
electronics = "electronics"
home_appliances = "home_appliances"
@classmethod
def _missing_(cls, value):
if isinstance(value, str):
value_lower = value.lower()
for member in cls:
if member.value == value_lower:
return member
return None
class Detailsbase(SQLModel):
brand: str
warranty: str
color: str
weight: float = Field(default=0.0, nullable=False)
dimensions: float = Field(default=0.0, nullable=False)
class details(Detailsbase,table=True):
id :int= Field(default = None,primary_key=True)
product: "productresponse" = Relationship(back_populates='details')
product_id:int=Field(default=None,foreign_key='productresponse.id')
class Product(SQLModel):
name :str
price: float
Category: ProductCategory
in_stock: bool
class productcreate(Product):
details: list["Detailsbase"] | None = None
@validator('Category',pre=True)
def validate_category(cls,value):
return value.lower()
class productresponse(Product,table=True):
id :int= Field(default = None,primary_key=True)
details: list["details"] =Relationship(back_populates='product')