49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
from pydantic import BaseModel, Field
|
|
|
|
# Base schema
|
|
class LakeBase(BaseModel):
|
|
name: str = Field(..., description="Name of the lake")
|
|
location: str = Field(..., description="Location of the lake")
|
|
description: str | None = Field(None, description="Description of the lake")
|
|
|
|
class Config:
|
|
schema_extra = {
|
|
"example": {
|
|
"name": "Lake Tahoe",
|
|
"location": "California/Nevada, USA",
|
|
"description": "A beautiful alpine lake on the California-Nevada border."
|
|
}
|
|
}
|
|
|
|
# Schema for creating a new Lake
|
|
class LakeCreate(LakeBase):
|
|
pass
|
|
|
|
# Schema for Lake responses
|
|
class Lake(LakeBase):
|
|
id: int
|
|
name: str = Field(..., description="Name of the lake")
|
|
location: str = Field(..., description="Location of the lake")
|
|
description: str | None = Field(None, description="Description of the lake")
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
schema_extra = {
|
|
"example": {
|
|
"id": 1,
|
|
"name": "Lake Tahoe",
|
|
"location": "California/Nevada, USA",
|
|
"description": "A beautiful alpine lake on the California-Nevada border."
|
|
}
|
|
}
|
|
```
|
|
|
|
This code defines three Pydantic models for the Lake entity:
|
|
|
|
1. `LakeBase`: A base model that defines the common fields for `name`, `location`, and `description`.
|
|
2. `LakeCreate`: A model that inherits from `LakeBase` and is used for creating a new Lake instance.
|
|
3. `Lake`: A model that inherits from `LakeBase` and includes an `id` field. This model is used for representing Lake instances in API responses.
|
|
|
|
The `LakeBase` model includes example values in its `Config` class, which can be used for documentation purposes.
|
|
|
|
The `Lake` model sets `orm_mode=True` in its `Config` class, which allows it to work with ORMs (Object-Relational Mappers) and handle reading data from a database. It also includes an example value for the `id` field. |