diff --git a/worf/views/detail.py b/worf/views/detail.py index 0ac0415..0b5022d 100644 --- a/worf/views/detail.py +++ b/worf/views/detail.py @@ -1,16 +1,16 @@ from django.core.exceptions import ImproperlyConfigured, ValidationError from django.db import models from django.db.utils import IntegrityError +from django.shortcuts import get_object_or_404 from worf.casing import snake_to_camel -from worf.shortcuts import get_instance_or_http404 from worf.views.base import AbstractBaseAPI class DetailAPI(AbstractBaseAPI): lookup_field = "id" lookup_url_kwarg = "id" - instance = None + queryset = None def get(self, request, *args, **kwargs): return self.render_to_response() @@ -23,14 +23,16 @@ def serialize(self): raise ImproperlyConfigured(f"{serializer} did not return a dictionary") return payload + def get_queryset(self): + return (self.queryset or self.model.objects).all() + def get_instance(self): - # TODO support multiple lookup_fields self.lookup_kwargs = {self.lookup_field: self.kwargs[self.lookup_url_kwarg]} self.validate_lookup_field_values() - if self.instance is None: - self.instance = get_instance_or_http404(self.model, **self.lookup_kwargs) + if not hasattr(self, "instance"): + self.instance = get_object_or_404(self.get_queryset(), **self.lookup_kwargs) return self.instance