Skip to content

API Reference

Dynamically registers CRUD routes for Django models using Django Ninja.

This class scans installed Django models (excluding those from specified apps) and automatically creates/uses Pydantic schemas for listing, detailing, creating, and updating models. It registers these routes with Ninja.

Initializes the DynamicAPI instance.

Parameters:

Name Type Description Default
api NinjaAPI

The NinjaAPI instance.

required
exclude Optional[Dict[str, Union[bool, Set[str], Any]]]

Configuration for model/app exclusions.

None
schema_config Optional[Dict[str, Dict[str, List[str]]]]

Dictionary mapping model names to schema configurations (e.g., exclude fields and optional fields).

None
custom_schemas Optional[Dict[str, Dict[str, Type[BaseModel]]]]

Dictionary mapping model names to custom Pydantic Schema classes for list, detail, create, and update operations. The dictionary should have the structure: {"ModelName": {"list": ListSchema, "detail": DetailSchema, "create": CreateSchema, "update": UpdateSchema}} If a schema is not provided for a specific operation, the default generated schema will be used.

None
pagination_type Optional[str]

Type of pagination to use ('limit-offset' or 'page-number'). If None, uses NINJA_PAGINATION_CLASS from settings.

None
file_fields Optional[Dict[str, List[str]]]

Dictionary mapping model names to lists of file field names (e.g., {"Product": ["image", "document"]}).

None
auto_detect_files bool

Whether to automatically detect file fields in models.

True
auto_multipart bool

Whether to automatically use multipart for models with file fields.

True
use_multipart Optional[Dict[str, Dict[str, bool]]]

Dictionary specifying which models should use multipart/form-data (e.g., {"Product": {"create": True, "update": True}}).

None
is_async bool

Whether to use async routes (default: True).

True
auth Optional[bool]

Enable built-in auth routes when True. If None, falls back to settings.LAZY_NINJA.get("auth", False).

None
auth_profile_model Optional[Type[Any]]

Optional Django model class for a user profile (OneToOne with the user). If not provided, can be set via settings.LAZY_NINJA["profile_model"] as "app_label.ModelName".

None
auth_access_cookie_name str

Name of the access token cookie.

'lazy_ninja_access_token'
auth_refresh_cookie_name str

Name of the refresh token cookie.

'lazy_ninja_refresh_token'
auth_cookie_path str

Cookie path for auth cookies.

'/'
auth_tags Optional[List[str]]

Optional list of tags for auth endpoints.

None
Pagination Configuration

The pagination can be configured in three ways (in order of precedence): 1. pagination_type parameter in DynamicAPI 2. NINJA_PAGINATION_CLASS in Django settings 3. Default to LimitOffsetPagination (if no settings or parameter are provided)

The page size is configured via NINJA_PAGINATION_PER_PAGE in settings.

init()

Initializes the DynamicAPI.

This method acts as the public initializer for the library. It internally calls register_all_models() to scan models and register their corresponding CRUD routes.

register_all_models()

Scans Django models and registers routes.

Excludes models from specified apps. Uses custom schemas if provided; otherwise, generates schemas based on schema_config or defaults.

BasePagination

Bases: ABC

Base class for pagination strategies.

get_pagination_class_name() abstractmethod

Get the name of the pagination class for schema generation.

get_paginator() abstractmethod

Get the Django Ninja paginator class.

LimitOffsetPaginationStrategy

Bases: BasePagination

Limit-offset based pagination strategy.

PageNumberPaginationStrategy

Bases: BasePagination

Page number based pagination strategy.

get_default_pagination_class()

Get the default pagination class from Django Ninja settings.

Returns:

Type Description
Type[PaginationBase]

The configured pagination class or LimitOffsetPagination as fallback

get_pagination_strategy(pagination_type=None)

Factory function to get the appropriate pagination strategy.

Parameters:

Name Type Description Default
pagination_type Optional[str]

Either 'limit-offset', 'page-number', or None to use Django settings

None

Returns:

Type Description
BasePagination

A pagination strategy instance

Note

Pagination configuration priority: 1. pagination_type parameter if provided 2. NINJA_PAGINATION_CLASS from Django settings 3. LimitOffsetPagination as fallback

To configure the page size, set NINJA_PAGINATION_PER_PAGE in your Django settings. Example: NINJA_PAGINATION_PER_PAGE = 20 # Sets default page size to 20