39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
```python
|
|
from typing import Optional
|
|
from pydantic import BaseModel, EmailStr
|
|
|
|
class UserBase(BaseModel):
|
|
email: Optional[EmailStr] = None
|
|
is_active: Optional[bool] = True
|
|
is_superuser: bool = False
|
|
full_name: Optional[str] = None
|
|
|
|
class UserCreate(UserBase):
|
|
email: EmailStr
|
|
password: str
|
|
|
|
class UserUpdate(UserBase):
|
|
password: Optional[str] = None
|
|
|
|
class UserInDBBase(UserBase):
|
|
id: Optional[int] = None
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
class User(UserInDBBase):
|
|
pass
|
|
|
|
class UserInDB(UserInDBBase):
|
|
hashed_password: str
|
|
```
|
|
|
|
|
|
1. `UserBase`: Base model for user data with optional fields like `email`, `is_active`, `is_superuser`, and `full_name`.
|
|
2. `UserCreate`: Model for creating a new user, inheriting from `UserBase` and requiring `email` and `password` fields.
|
|
3. `UserUpdate`: Model for updating an existing user, inheriting from `UserBase` and allowing an optional `password` field.
|
|
4. `UserInDBBase`: Base model for user data retrieved from the database, inheriting from `UserBase` and adding an optional `id` field. It also sets the `orm_mode` configuration for working with SQLAlchemy models.
|
|
5. `User`: Model representing a user object retrieved from the database, inheriting from `UserInDBBase`.
|
|
6. `UserInDB`: Model representing a user object with a `hashed_password` field, inheriting from `UserInDBBase`.
|
|
|
|
These models can be used in various parts of the FastAPI application, such as request/response bodies, database operations, and data validation. Make sure to import the necessary dependencies (`pydantic`, `typing`, and `email_validator`) at the top of the file if they are not already imported. |