Skip to content

Unclear how to properly use the get_queryset() #40

@Jakuje

Description

@Jakuje

All the examples directly set queryset for views. The documentation says that I should override the get_queryset() on them, but call super().get_queryset(), but this really does not work if we do not have the base class that would stop propagation of this call to the rest_framework's get_queryset(), which asserts the queryset is set.

https://github.com/encode/django-rest-framework/blob/e454758fb6edf1dcf5aa5417a388b940c871469c/rest_framework/generics.py#L67

I really liked the concept of this project trying to avoid all the mess with splitting queries and models, but only way how I was able to make this working was something like this:

class BaseVirtualModelListApiView(ListAPIView):
    def get_queryset(self):
        # make sure the ListApiView.get_queryset() is not called
        return self.model.objects

class MyVirtualModelListApiView(v.GenericVirtualModelViewMixin, BaseVirtualModelListApiView):
    pass

class BlogPostsView(MyVirtualModelListApiView):
    model = Blog

Not sure if I am just stupid or there is some better way. In any case, it would be good do document how this should work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions