Skip to content

API reference

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 inferred 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.

  • ordering (tuple) –

    The fields used to sort the queryset. Defaults to the model ordering, completed by the primary key so that pagination is stable.

  • linearization_tolerance (float | None) –

    The largest distance, in the unit of the storage CRS, between an arc and the segments that stand for it when a client asks for the curves linearized. Defaults to None, which draws a quarter of a circle with 32 segments, as PostGIS does.

curved cached property

curved: bool

Whether the geometry column holds curves only, like a CURVEPOLYGON one, rather than any geometry.

storage_crs

storage_crs() -> CRS | None

The CRS the geometries are stored in, or None for a collection without geometry.

supported_crs

supported_crs() -> tuple[CRS, ...]

Hook for specifying which CRS the collection can be queried in.

linearize

linearize(qs: QuerySet[M], crs: CRS) -> QuerySet[M]

Linearize the curves of a queryset (as produced by query(), in crs), for GeoJSON: in the storage CRS, where the arcs are drawn, and then reprojected. PostGIS fails on an empty CurvePolygon, which has none.

get_queryset

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

Return the model queryset.

get_ordering

get_ordering(request) -> tuple

Hook for specifying field ordering.

The model ordering is kept, with the primary key appended as a tie breaker: without one, rows that compare equal can move between pages and be returned twice or not at all.

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.

get_geometry_schema cached

get_geometry_schema(linearized: bool = False) -> type[Geometry] | None

The schema of the geometries of the collection, or with linearized of the GeoJSON geometries that curves are linearized to.

get_jsonfg_feature_output_schema

get_jsonfg_feature_output_schema(request: HttpRequest, *, root: bool = False) -> type[JsonFgFeature]

The schema of JSON-FG features, of the root of a document when served on their own.

get_queryables

get_queryables(request: HttpRequest) -> tuple[str, ...]

Hook for specifying the properties the items can be filtered on, among the fields: the exposed ones and the geometry by default.

get_queryables_schema

get_queryables_schema(request: HttpRequest) -> dict

The schema of the queryables, the one of the features restricted to them. The geometry also refers to its GeoJSON schema: QGIS only recognizes a geometry queryable by it, and would filter them itself otherwise.

queryset_to_featurecollection

queryset_to_featurecollection(request: HttpRequest, qs: QuerySet, *, number_matched: int | None = None, links: list[OAPIFLink] | None = None, crs: CRS = CRS84, profile: Profile | None = None) -> FeatureCollection

Convert a queryset (as produced by query(), in crs) to a FeatureCollection in a GeoJSON profile, by default a JSON-FG one when it has curves, which GeoJSON cannot carry: they are left out of GeoJSON, unless linearize() made lines of them. number_matched defaults to the number of features returned, for a queryset that is not a page of a larger one.

get_arrow_properties_schema

get_arrow_properties_schema(properties_schema: type[Schema]) -> tuple[Schema, dict[str, str]]

Arrow schema of the feature properties, derived from their types so that every page of a collection shares one, and where the values of each column come from: "python" for the types Arrow has, "json" for the others, written as in the GeoJSON, and "json_text" for the ones written as JSON text.

queryset_to_arrow_stream

queryset_to_arrow_stream(request: HttpRequest, qs: QuerySet, crs: CRS)

Convert a queryset (as produced by query()) to a pyarrow Table with a GeoArrow-WKB geometry column.

model_to_feature

model_to_feature(request: HttpRequest, obj: M, *, crs: CRS = CRS84, profile: Profile | None = None) -> Feature

Convert a row (as produced by query(), in crs) to a Feature in a GeoJSON profile, by default a JSON-FG one for a curve.

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.

django_oapif.auth

BasicAuth

Bases: HttpBasicAuth

HTTP Basic authentication, checked against the authentication backends of Django.

DjangoAuth

Bases: APIKeyCookie

The user of the Django session, or the anonymous user without one.

django_oapif.crs

CRS

CRS(auth: str, srid: int)

A coordinate reference system, by authority and code: CRS("EPSG", 2056), or CRS("OGC", 4326) for CRS84.

auth_code

auth_code() -> str

Authority code, as GeoArrow expects it in the CRS metadata of a geometry column.