45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
Ensure all necessary imports and dependencies are included, maintaining consistency with a FastAPI project structure using SQLite and SQLAlchemy.
|
|
|
|
Here's the `user.py` file for the `app/api/v1/schemas/` directory:
|
|
|
|
```python
|
|
from typing import Optional
|
|
from pydantic import BaseModel, EmailStr
|
|
|
|
# User Schema for Pydantic Model
|
|
class UserBase(BaseModel):
|
|
email: EmailStr
|
|
is_active: Optional[bool] = True
|
|
is_superuser: bool = False
|
|
full_name: Optional[str] = None
|
|
|
|
# Properties to receive via API on creation
|
|
class UserCreate(UserBase):
|
|
email: EmailStr
|
|
password: str
|
|
|
|
# Properties to receive via API on update
|
|
class UserUpdate(UserBase):
|
|
password: Optional[str] = None
|
|
|
|
# Additional properties to return via API
|
|
class User(UserBase):
|
|
id: int
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
```
|
|
|
|
Explanation:
|
|
|
|
1. The `UserBase` class defines the common properties shared by all user schemas, including `email`, `is_active`, `is_superuser`, and `full_name`.
|
|
|
|
2. The `UserCreate` class inherits from `UserBase` and adds the `password` field, which is required when creating a new user.
|
|
|
|
3. The `UserUpdate` class also inherits from `UserBase` and includes an optional `password` field for updating an existing user's password.
|
|
|
|
4. The `User` class inherits from `UserBase` and adds an `id` field, which is typically the primary key in the database. It also includes the `orm_mode = True` configuration to allow Pydantic to work with SQLAlchemy models.
|
|
|
|
5. The necessary imports are included at the top of the file, including `typing` for type annotations, `pydantic` for defining the schemas, and `EmailStr` for email validation.
|
|
|
|
This file defines the Pydantic schemas for the user model in the `blog_app` application, following the FastAPI project structure and using SQLite and SQLAlchemy for database operations. |