Skip to content

Align overwriting of get_queryset in example app and documentation #979

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

Merged
merged 1 commit into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,14 @@ class LineItemViewSet(viewsets.ModelViewSet):
serializer_class = LineItemSerializer

def get_queryset(self):
queryset = super(LineItemViewSet, self).get_queryset()
queryset = super().get_queryset()

# if this viewset is accessed via the 'order-lineitems-list' route,
# it wll have been passed the `order_pk` kwarg and the queryset
# needs to be filtered accordingly; if it was accessed via the
# unnested '/lineitems' route, the queryset should include all LineItems
if 'order_pk' in self.kwargs:
order_pk = self.kwargs['order_pk']
order_pk = self.kwargs.get('order_pk')
if order_pk is not None:
queryset = queryset.filter(order__pk=order_pk)

return queryset
Expand Down
6 changes: 4 additions & 2 deletions example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,13 @@ class CommentViewSet(ModelViewSet):
}

def get_queryset(self, *args, **kwargs):
queryset = super().get_queryset()

entry_pk = self.kwargs.get("entry_pk", None)
if entry_pk is not None:
return self.queryset.filter(entry_id=entry_pk)
queryset = queryset.filter(entry_id=entry_pk)

return super(CommentViewSet, self).get_queryset()
return queryset


class CompanyViewset(ModelViewSet):
Expand Down