Created
November 23, 2022 07:39
-
-
Save AliRn76/7bf2bf7b01277eb08db1972cbf2f5f20 to your computer and use it in GitHub Desktop.
Django Custom Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from rest_framework.status import HTTP_404_NOT_FOUND | |
from rest_framework.exceptions import APIException | |
class BaseManager(models.Manager): | |
def get_or_raise(self, *args, **kwargs): | |
queryset = super().get_queryset() | |
try: | |
return queryset.get(*args, **kwargs) | |
except queryset.model.DoesNotExist: | |
raise APIException(code=HTTP_404_NOT_FOUND, detail=f'{queryset.model._meta.object_name} Does Not Exist') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment