39 lines
962 B
Python
39 lines
962 B
Python
Here's the `posts.py` file with Pydantic schemas for posts, following the FastAPI project structure with SQLite and SQLAlchemy:
|
|
|
|
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
from pydantic import BaseModel
|
|
|
|
class PostBase(BaseModel):
|
|
title: str
|
|
content: str
|
|
|
|
class PostCreate(PostBase):
|
|
pass
|
|
|
|
class PostUpdate(PostBase):
|
|
pass
|
|
|
|
class PostInDBBase(PostBase):
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
class Post(PostInDBBase):
|
|
pass
|
|
|
|
class PostInDB(PostInDBBase):
|
|
pass
|
|
|
|
Explanation:
|
|
|
|
1. We import the necessary modules: `typing` for type hints, `datetime` for dealing with date and time objects, and `pydantic` for defining data models.
|
|
|
|
|
|
|
|
|
|
5. `PostInDBBase` inherits from `PostBase` and includes additional fields: `id`, `created_at`, and `updated_at`. It also sets the `orm_mode` configuration to `True` for compatibility with SQLAlchemy models. |