|
| 1 | +# Model Configuration |
| 2 | + |
| 3 | +The `ModelConfig` class in Ninja Extra provides extensive configuration options for Model Controllers. It allows you to customize schema generation, route behavior, and pagination settings. |
| 4 | + |
| 5 | +## **Basic Configuration** |
| 6 | + |
| 7 | +Here's a comprehensive example of `ModelConfig` usage: |
| 8 | + |
| 9 | +```python |
| 10 | +from ninja_extra import ( |
| 11 | + ModelConfig, |
| 12 | + ModelControllerBase, |
| 13 | + ModelSchemaConfig, |
| 14 | + api_controller, |
| 15 | +) |
| 16 | +from .models import Event |
| 17 | + |
| 18 | +@api_controller("/events") |
| 19 | +class EventModelController(ModelControllerBase): |
| 20 | + model_config = ModelConfig( |
| 21 | + model=Event, |
| 22 | + schema_config=ModelSchemaConfig( |
| 23 | + read_only_fields=["id", "created_at"], |
| 24 | + write_only_fields=["password"], |
| 25 | + include=["title", "start_date", "end_date", "category"], |
| 26 | + exclude=set(), # Fields to exclude |
| 27 | + depth=1, # Nesting depth for related fields |
| 28 | + ), |
| 29 | + async_routes=False, # Enable/disable async routes |
| 30 | + allowed_routes=["create", "find_one", "update", "patch", "delete", "list"], |
| 31 | + ) |
| 32 | +``` |
| 33 | + |
| 34 | +## **Schema Configuration** |
| 35 | + |
| 36 | +The `ModelSchemaConfig` class controls how Pydantic schemas are generated from your Django models: |
| 37 | + |
| 38 | +```python |
| 39 | +from ninja_extra import ModelConfig, ModelSchemaConfig |
| 40 | + |
| 41 | +# Detailed schema configuration |
| 42 | +schema_config = ModelSchemaConfig( |
| 43 | + # Include specific fields (use "__all__" for all fields) |
| 44 | + include=["title", "description", "start_date"], |
| 45 | + |
| 46 | + # Exclude specific fields |
| 47 | + exclude={"internal_notes", "secret_key"}, |
| 48 | + |
| 49 | + # Fields that should be read-only (excluded from create/update schemas) |
| 50 | + read_only_fields=["id", "created_at", "updated_at"], |
| 51 | + |
| 52 | + # Fields that should be write-only (excluded from retrieve schemas) |
| 53 | + write_only_fields=["password"], |
| 54 | + |
| 55 | + # Depth of relationship traversal |
| 56 | + depth=1, |
| 57 | + |
| 58 | + # Additional Pydantic config options |
| 59 | + extra_config_dict={ |
| 60 | + "title": "EventSchema", |
| 61 | + "description": "Schema for Event model", |
| 62 | + "populate_by_name": True |
| 63 | + } |
| 64 | +) |
| 65 | + |
| 66 | +model_config = ModelConfig( |
| 67 | + model=Event, |
| 68 | + schema_config=schema_config |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +## **Custom Schemas** |
| 73 | + |
| 74 | +You can provide your own Pydantic schemas instead of using auto-generated ones: |
| 75 | + |
| 76 | +```python |
| 77 | +from datetime import date |
| 78 | +from pydantic import BaseModel, Field |
| 79 | + |
| 80 | +class EventCreateSchema(BaseModel): |
| 81 | + title: str = Field(..., max_length=100) |
| 82 | + start_date: date |
| 83 | + end_date: date |
| 84 | + category_id: int | None = None |
| 85 | + |
| 86 | +class EventRetrieveSchema(BaseModel): |
| 87 | + id: int |
| 88 | + title: str |
| 89 | + start_date: date |
| 90 | + end_date: date |
| 91 | + category_id: int | None |
| 92 | + |
| 93 | +@api_controller("/events") |
| 94 | +class EventModelController(ModelControllerBase): |
| 95 | + model_config = ModelConfig( |
| 96 | + model=Event, |
| 97 | + create_schema=EventCreateSchema, |
| 98 | + retrieve_schema=EventRetrieveSchema, |
| 99 | + update_schema=EventCreateSchema, # Reuse create schema for updates |
| 100 | + ) |
| 101 | +``` |
| 102 | + |
| 103 | +## **Pagination Configuration** |
| 104 | + |
| 105 | +Model Controllers support customizable pagination for list endpoints: |
| 106 | + |
| 107 | +```python |
| 108 | +from ninja.pagination import LimitOffsetPagination |
| 109 | +from ninja_extra import ( |
| 110 | + ModelConfig, |
| 111 | + ModelPagination |
| 112 | +) |
| 113 | +from ninja_extra.pagination import NinjaPaginationResponseSchema |
| 114 | + |
| 115 | +@api_controller("/events") |
| 116 | +class EventModelController(ModelControllerBase): |
| 117 | + model_config = ModelConfig( |
| 118 | + model=Event, |
| 119 | + # Configure pagination |
| 120 | + pagination=ModelPagination( |
| 121 | + klass=LimitOffsetPagination, |
| 122 | + pagination_schema=NinjaPaginationResponseSchema, |
| 123 | + paginator_kwargs={ |
| 124 | + "limit": 20, |
| 125 | + "offset": 100 |
| 126 | + } |
| 127 | + ) |
| 128 | + ) |
| 129 | +``` |
| 130 | + |
| 131 | +## **Route Configuration** |
| 132 | + |
| 133 | +You can customize individual route behavior using route info dictionaries: |
| 134 | + |
| 135 | +```python |
| 136 | +@api_controller("/events") |
| 137 | +class EventModelController(ModelControllerBase): |
| 138 | + model_config = ModelConfig( |
| 139 | + model=Event, |
| 140 | + # Customize specific route configurations |
| 141 | + create_route_info={ |
| 142 | + "summary": "Create a new event", |
| 143 | + "description": "Creates a new event with the provided data", |
| 144 | + "tags": ["events"], |
| 145 | + "deprecated": False, |
| 146 | + }, |
| 147 | + list_route_info={ |
| 148 | + "summary": "List all events", |
| 149 | + "description": "Retrieves a paginated list of all events", |
| 150 | + "tags": ["events"], |
| 151 | + }, |
| 152 | + find_one_route_info={ |
| 153 | + "summary": "Get event details", |
| 154 | + "description": "Retrieves details of a specific event", |
| 155 | + "tags": ["events"], |
| 156 | + } |
| 157 | + ) |
| 158 | +``` |
| 159 | + |
| 160 | +## **Async Routes Configuration** |
| 161 | + |
| 162 | +Enable async routes and configure async behavior: |
| 163 | + |
| 164 | +```python |
| 165 | +@api_controller("/events") |
| 166 | +class AsyncEventModelController(ModelControllerBase): |
| 167 | + model_config = ModelConfig( |
| 168 | + model=Event, |
| 169 | + # Async-specific configurations |
| 170 | + async_routes=True, |
| 171 | + schema_config=ModelSchemaConfig( |
| 172 | + read_only_fields=["id"], |
| 173 | + depth=1 |
| 174 | + ) |
| 175 | + ) |
| 176 | + |
| 177 | + # Custom async service implementation |
| 178 | + service = AsyncEventModelService(model=Event) |
| 179 | +``` |
| 180 | + |
| 181 | +## **Configuration Inheritance** |
| 182 | + |
| 183 | +ModelConfig also support configuration inheritance: |
| 184 | + |
| 185 | +```python |
| 186 | +from ninja_extra.controllers import ModelConfig |
| 187 | + |
| 188 | +class BaseModelConfig(ModelConfig): |
| 189 | + async_routes = True |
| 190 | + schema_config = ModelSchemaConfig( |
| 191 | + read_only_fields=["id", "created_at", "updated_at"], |
| 192 | + depth=1 |
| 193 | + ) |
| 194 | + |
| 195 | +@api_controller("/events") |
| 196 | +class EventModelController(ModelControllerBase): |
| 197 | + model_config = BaseModelConfig( |
| 198 | + model=Event, |
| 199 | + # Override or extend base configuration |
| 200 | + allowed_routes=["list", "find_one"] |
| 201 | + ) |
| 202 | +``` |
| 203 | + |
| 204 | +## **Best Practices** |
| 205 | + |
| 206 | +1. **Schema Configuration**: |
| 207 | + - Always specify `read_only_fields` for auto-generated fields |
| 208 | + - Use `depth` carefully as it can impact performance |
| 209 | + - Consider using `exclude` for sensitive fields |
| 210 | + |
| 211 | +2. **Route Configuration**: |
| 212 | + - Limit `allowed_routes` to only necessary endpoints |
| 213 | + - Provide meaningful summaries and descriptions |
| 214 | + - Use tags for API organization |
| 215 | + |
| 216 | +3. **Pagination**: |
| 217 | + - Always set reasonable limits |
| 218 | + - Consider your data size when choosing pagination class |
| 219 | + - Use appropriate page sizes for your use case |
| 220 | + |
| 221 | +4. **Async Support**: |
| 222 | + - Enable `async_routes` when using async database operations |
| 223 | + - Implement custom async services for complex operations |
| 224 | + - Consider performance implications of async operations |
0 commit comments