Duplicate queries are the result get_object_or_404().
If you want to extract the parent object from the arguments of the URL keyword, you can set it as an attribute in the submit method, which is called only once, and then access the object already received in get_queryset().
from rest_framework.exceptions import NotFound
class PropertyPledgeList(generics.ListAPIView):
queryset = PropertyPledge.objects.all()
serializer_class = PledgeListSerializer
def dispatch(self, request, *args, **kwargs):
try:
self.property = Property.objects.get(id=kwargs['slug'])
except Property.DoesNotExist:
self.property = None
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
if not self.property:
raise NotFound
return self.property.get_pledges()
source
share