Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom queryset on detail views #68

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions worf/views/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from django.db.utils import IntegrityError

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()
Expand All @@ -23,14 +22,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 = self.get_queryset().get(**self.lookup_kwargs)

return self.instance

Expand Down