40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
from typing import Optional
|
||
from sqlalchemy import Column, Integer, String, DECIMAL, DateTime
|
||
from sqlalchemy.sql import func
|
||
from pydantic import BaseModel, Field
|
||
from .database import Base
|
||
|
||
# 数据库模型
|
||
class CommunityDB(Base):
|
||
__tablename__ = "communities"
|
||
|
||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||
name = Column(String(100), nullable=False)
|
||
address = Column(String(200), nullable=False)
|
||
longitude = Column(DECIMAL(9,6), nullable=False) # 经度,精确到小数点后6位
|
||
latitude = Column(DECIMAL(9,6), nullable=False) # 纬度,精确到小数点后6位
|
||
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||
|
||
# Pydantic 模型
|
||
class CommunityCreate(BaseModel):
|
||
name: str = Field(..., max_length=100)
|
||
address: str = Field(..., max_length=200)
|
||
longitude: float = Field(..., ge=-180, le=180)
|
||
latitude: float = Field(..., ge=-180, le=180)
|
||
|
||
class CommunityUpdate(BaseModel):
|
||
name: Optional[str] = Field(None, max_length=100)
|
||
address: Optional[str] = Field(None, max_length=200)
|
||
longitude: Optional[float] = Field(None, ge=-180, le=180)
|
||
latitude: Optional[float] = Field(None, ge=-180, le=180)
|
||
|
||
class CommunityInfo(BaseModel):
|
||
id: int
|
||
name: str
|
||
address: str
|
||
longitude: float
|
||
latitude: float
|
||
|
||
class Config:
|
||
from_attributes = True |