Skip to content

API documentation

django_oapif

OAPIF

OAPIF(*, title: str = 'OAPIF', version: str = version('django-oapif'), description: str = '', docs: DocsBase = Swagger(), docs_url: str | None = '/docs', docs_decorator: Callable[[TCallable], TCallable] | None = None, servers: list[DictStrAny] | None = None, urls_namespace: str | None = None, auth: Sequence[Callable] | Callable | NOT_SET_TYPE | None = NOT_SET, throttle: BaseThrottle | list[BaseThrottle] | NOT_SET_TYPE = NOT_SET, renderer: BaseRenderer | None = None, parser: Parser | None = None, default_router: Router | None = None, openapi_extra: dict[str, Any] | None = None)

The OAPIF class wraps a Ninja API that will expose the OGC API Features endpoints.

The parameters will be passed to the NinjaApi constructor. By default auth will be set to use the same authentication method as Django, but it may be configured differently (see Ninja authentication documentation). Django OAPIF also provides a utility to use HTTP Basic Auth as an authentication method.

Examples:

>>> from django_oapif import OAPIF
>>> from django_oapif.auth import BasicAuth, DjangoAuth
>>>
>>> api = OAPIF(auth=[BasicAuth(), DjangoAuth()])

register_collection

register_collection(models: type[Model] | Iterable[type[Model]], oapif_class: type[OapifCollection] = OapifCollection) -> None

Register a model to expose as an OGC API Features collection.

register

register(*models: type[Model]) -> Callable[[type[T]], type[T]]

Register a model to expose as an OGC API Features collection.

OapifCollection

OapifCollection(model: type[M])

Base class used to customize authorization and model operations.

Attributes:

  • id (str) –

    The collection identifier when calling the API, eg: https://example.com/oapif/collections/<id>/items. If not defined, will be set to model_class._meta.label_lower.

  • title (str) –

    The collection title. If not defined, will be set to model_class._meta.label.

  • description (str | None) –

    The collection description.

  • geometry_field (str | None) –

    The collection geometry field. If not defined, the geometry field will be infered from the model.

  • fields (tuple[str, ...]) –

    The list of fields that will be exposed as feature properties. If not defined, all fields will be used.

  • readonly_fields (tuple[str, ...]) –

    The list of fields that will be included in the feature properties, but won't be accepted in Create/Update operations.

  • exclude (tuple[str, ...]) –

    The list of fields to be excluded from the feature properties.exclude:

  • ordering

    The field used to sort the queryset.

get_queryset

get_queryset(request: HttpRequest) -> QuerySet[M]

Return the model queryset.

get_ordering

get_ordering(request) -> tuple

Hook for specifying field ordering.

get_fields

get_fields(request, obj=None) -> tuple[str, ...]

Hook for specifying fields.

get_exclude

get_exclude(request, obj=None) -> tuple[str, ...]

Hook for specifying fields.

get_readonly_fields

get_readonly_fields(request, obj=None) -> tuple[str, ...]

Hook for specifying custom readonly fields.

save_model

save_model(_request: HttpRequest, obj: M, _change: bool) -> None

Given a model instance save it to the database.

delete_model

delete_model(_request: HttpRequest, obj: M) -> tuple[int, dict[str, int]]

Given a model instance delete it from the database.

has_view_permission

has_view_permission(request: HttpRequest, _obj: M | None = None) -> bool

Returns True if the given request has permission to view objects in the collection, or a given object if defined.

has_add_permission

has_add_permission(request: HttpRequest, _obj: M | None = None) -> bool

Returns True if the given request has permission to create objects in the collection, or a given object if defined.

has_change_permission

has_change_permission(request: HttpRequest, _obj: M | None = None) -> bool

Returns True if the given request has permission to change objects in the collection, or a given object if defined.

has_delete_permission

has_delete_permission(request: HttpRequest, _obj: M | None = None) -> bool

Returns True if the given request has permission to delete objects in the collection, or a given object if defined.

AllowAnyCollection

AllowAnyCollection(model: type[M])

Bases: OapifCollection

Allows full access to everyone.

AuthenticatedCollection

AuthenticatedCollection(model: type[M])

Bases: OapifCollection

Allows full access to authenticated users only.

AuthenticatedOrReadOnlyCollection

AuthenticatedOrReadOnlyCollection(model: type[M])

Bases: AuthenticatedCollection

Allows full access to authenticated users only, but allows readonly access to everyone.

AnonReadOnlyCollection

AnonReadOnlyCollection(model: type[M])

Bases: OapifCollection

Reuses all Django permissions for a given model, but allows readonly access to everyone.