Skip to content

Instantly share code, notes, and snippets.

@AliRn76
Created November 23, 2022 07:39
Show Gist options
  • Save AliRn76/7bf2bf7b01277eb08db1972cbf2f5f20 to your computer and use it in GitHub Desktop.
Save AliRn76/7bf2bf7b01277eb08db1972cbf2f5f20 to your computer and use it in GitHub Desktop.
Django Custom Manager
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