Utils: add a utility for getting objects or 404ing.
parent
e7721e8fe5
commit
e686f01652
@ -0,0 +1,13 @@
|
|||||||
|
from fastapi import status
|
||||||
|
|
||||||
|
from django.db.models import QuerySet
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
|
from .exceptions import ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
def get_object_or_404(queryset: QuerySet, **kwargs):
|
||||||
|
try:
|
||||||
|
return queryset.get(**kwargs)
|
||||||
|
except ObjectDoesNotExist as e:
|
||||||
|
raise ValidationError("does_not_exist", str(e), status_code=status.HTTP_404_NOT_FOUND)
|
Loading…
Reference in New Issue