3 ms·
I like the automatic model serializers, but don't like the extra layer of syntax in views, ie different than normal Django. DRF's system can be replaced in many
by the__alchemist 3y ago
I like the automatic model serializers, but don't like the extra layer of syntax in views, ie different than normal Django. DRF's system can be replaced in many cases by these helper functions:
def return_json(msg: dict) -> HttpResponse:
return HttpResponse(json.dumps(msg), content_type="application/json")
def load_body(request: HttpRequest) -> dict:
return json.loads(request.body.decode("utf-8"))
This avoids the cognitive overhead of layering another syntax on top of Django's own conventions.
- jdboyd 3y agoIsn't the former basically a JsonResponse? https://docs.djangoproject.com/en/5.0/ref/request-response/#jsonresponse-objects https://docs.djangoproject.com/en/5.0/ref/request-response/#...
- the__alchemist 3y agoApparently; even better; ty! Edit: Did a drop-in replacement of JsonResponse; confirmed working.