Defining OPTIONS or meta method¶
RFC 9110
defines the OPTIONS HTTP method, but sadly Django’s
View which we use as a base class
for all controllers, already has
options() method.
It would generate a typing error to redefine it with a different signature that we need for our endpoints.
That’s why we created our own meta controller method as a replacement
for older Django’s options name.
To use it you have two options:
Define the
metaendpoint yourself and provide a custom implementationUse
MetaMixinorAsyncMetaMixinwith the default implementation: we provideAllowheader with all the allowed HTTP methods in this controller
Here’s an example of a custom meta implementation:
views.py¶
1from http import HTTPStatus
2
3from django.http import HttpResponse
4
5from dmr import Controller, HeaderSpec, ResponseSpec, validate
6from dmr.plugins.msgspec import MsgspecSerializer
7
8
9class SettingsController(Controller[MsgspecSerializer]):
10 def get(self) -> str:
11 return 'default get setting'
12
13 def post(self) -> str:
14 return 'default post setting'
15
16 # `meta` response is also validated, schema is required:
17 @validate(
18 ResponseSpec(
19 None,
20 status_code=HTTPStatus.NO_CONTENT,
21 headers={'Allow': HeaderSpec()},
22 ),
23 )
24 def meta(self) -> HttpResponse: # Handles `OPTIONS` http method
25 return self.to_response(
26 None,
27 status_code=HTTPStatus.NO_CONTENT,
28 headers={
29 'Allow': ', '.join(
30 method for method in sorted(self.api_endpoints.keys())
31 ),
32 },
33 )
34
Run result
$ curl http://127.0.0.1:8000/api/settings/ -D - -X OPTIONS
HTTP/1.1 204 No Content
date: Sun, 05 Apr 2026 17:51:41 GMT
server: uvicorn
Allow: GET, OPTIONS, POST
Content-Type: application/json
X-Frame-Options: DENY
Vary: Accept-Language
Content-Language: en
Content-Length: 0
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin