2👍
The short answer is that you can’t do it there directly because the calling function still has to do something with the return value from get_record_for_model
. That said, I would recommend that you do something like the below, which sends data as well as a found/not found boolean back to the calling function:
def get_record_from_model(model, **kwargs):
try:
return model.objects.get(**kwargs), True
except model.DoesNotExist:
error_data = copy.copy(settings.ERROR["NOT_EXIST_ERR"])
return error_data, False
...
def bar(request):
...
data, found = get_record_from_model(model, **kwargs)
if not found:
return JsonResponse(data, status=404)
...
return JsonResponse(response_data)
0👍
Use django’s built-in shortcut get_object_or_404
from django.shortcuts import get_object_or_404
def bar(request):
...
record = get_object_or_404(model, **kwargs)
...
return JsonResponse(data_to_response)
- [Answered ]-How to send message from django server to a nodejs server and receive the reply?
- [Answered ]-Django REST Framework user registration Validation confusion
- [Answered ]-Django with Apache : Global variable in view.py
- [Answered ]-Django rest project dockerfile
Source:stackexchange.com