Pydantic Integration
Flask Pydantic ReDoc automatically generates your API schemas using Pydantic models. In this section, you will learn how to effectively use Pydantic models.
Pydantic Plugin
Flask Pydantic ReDoc uses a special plugin to convert Pydantic models to OpenAPI schemas. This plugin is implemented with the PydanticPlugin
class and correctly transfers all features of Pydantic models to OpenAPI schemas.
from flask_pydantic_redoc import Redoc
from pydantic import BaseModel, Field
app = Flask(__name__)
# Define your Pydantic models
class User(BaseModel):
id: int = Field(..., description="User ID")
username: str = Field(..., description="Username")
# Add your models when initializing Redoc
redoc = Redoc(app, schemas=[User])
Contents
You can find detailed information about Pydantic integration in the following sections:
- Model Examples - Pydantic model examples and OpenAPI schema conversions
- CRUD Operations - Using Pydantic models in API endpoints
Registering Models with Redoc
To register your models with Redoc:
from flask import Flask
from flask_pydantic_redoc import Redoc
app = Flask(__name__)
# Single model
redoc = Redoc(app, schemas=[User])
# Multiple models
redoc = Redoc(app, schemas=[User, Address, UserDetail, Product])
Best Practices
- Always add field descriptions (
description
) - Set default values appropriately
- Add necessary validations
- Organize nested models logically
- Use type hints correctly