`ListView.get_queryset` may return an iterable not just `QuerySet`
Quoting django:
class MultipleObjectMixin(ContextMixin):
"""A mixin for views manipulating multiple objects."""
def get_queryset(self):
"""
Return the list of items for this view.
The return value must be an iterable and may be an instance of
`QuerySet` in which case `QuerySet` specific behavior will be enabled.
"""
And it is later used like so:
class BaseListView(MultipleObjectMixin, View):
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
allow_empty = self.get_allow_empty()
if not allow_empty:
# When pagination is enabled and object_list is a queryset,
# it's better to do a cheap query than to load the unpaginated
# queryset in memory.
if self.get_paginate_by(self.object_list) is not None and hasattr(
self.object_list, "exists"
):
is_empty = not self.object_list.exists()
else:
is_empty = not self.object_list
In runtime - any Iterable works. So, we must fix the allowed return type of get_queryset
See django/views/generic/list.py
I want to try to make this task:)
I would revisit this comment first: https://github.com/typeddjango/django-stubs/pull/2174#issuecomment-2124394572
And the linked issues/PRs there. As iterables from get_queryset has been discussed previously
Thanks for the context, @flaeppe! I tried to find the discussion, but failed :)
This is a good point. Making get_queryset to return Iterable will break a lot of code that expects QuerySet return type.
My use-case is that RawQuerySet can't be returned from .get_queryset without a type error.
# type: ignore[override] does not seem really useful.
What are the possible ways to handle it in your opinion?
I don't have any good suggestions. Think I'd try to stick to what is returned runtime, if possible. But if Django mixes it probably becomes tedious quite fast, perhaps not really type friendly code