45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
from typing import Optional
|
|
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean
|
|
from sqlalchemy.sql import func
|
|
from pydantic import BaseModel, Field
|
|
from .database import Base
|
|
|
|
# 数据库模型
|
|
class AddressDB(Base):
|
|
__tablename__ = "delivery_addresses"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
user_id = Column(Integer, ForeignKey("users.userid"), index=True)
|
|
community_id = Column(Integer, index=True)
|
|
address_detail = Column(String(200))
|
|
name = Column(String(50))
|
|
phone = Column(String(11))
|
|
is_default = Column(Boolean, default=False)
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# Pydantic 模型
|
|
class AddressCreate(BaseModel):
|
|
community_id: int
|
|
address_detail: str = Field(..., max_length=200)
|
|
name: str = Field(..., max_length=50)
|
|
phone: str = Field(..., pattern="^1[3-9]\d{9}$")
|
|
is_default: bool = False
|
|
|
|
class AddressUpdate(BaseModel):
|
|
community_id: Optional[int] = None
|
|
address_detail: Optional[str] = Field(None, max_length=200)
|
|
name: Optional[str] = Field(None, max_length=50)
|
|
phone: Optional[str] = Field(None, pattern="^1[3-9]\d{9}$")
|
|
is_default: Optional[bool] = None
|
|
|
|
class AddressInfo(BaseModel):
|
|
id: int
|
|
community_id: int
|
|
address_detail: str
|
|
name: str
|
|
phone: str
|
|
is_default: bool
|
|
|
|
class Config:
|
|
from_attributes = True |