1

I'm working on Django app and currently ran into a problem. I have my ListView as:

class CategoryView(ListView):
  template_name = "categories.html"

  def get_queryset(self):
     ...
     ...
     queryset = {"category": parent, "items": items.all()}
     return queryset

Is there any walkaround for paginating "items" from queryset dict? Because when I set paginate_by = xx I get error unhashable type: 'slice'. What is, from my understanding, and it's pretty obvious cause by the fact that it doesn't know what if I request to paginate "category" or "items".

Thank you for every input.

Michal

Michal
  • 61
  • 1
  • 6
  • Your best option is to reorganize your logic so that you're not trying to return two things from `get_queryset` which should only return 1 thing. – schillingt Mar 06 '20 at 15:26
  • Honestly, I was thinking about that a lot before posting this question, but I can't find any way to do what I want to do with just one queryset. Even after reorganizing Models. – Michal Mar 06 '20 at 21:12
  • Please help us to understand why you want to set `category` in `get_queryset`. Why cannot you save `self.parent=parent` and set `context["parent"] = self.parent` in `get_context_data()` ? – ChrisRob Mar 06 '20 at 21:59

1 Answers1

0

when you use the get_queryset method it should return Queryset that will be used to retrieve the object that this view will display.

if you want passing multiple objects in queryset use get_context_data method


from django.views.generic.list import ListView

class CategoryView(ListView):
    template_name = "categories.html"
    model = Category # category model
    paginate_by = 100  # if pagination is desired

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['items'] = items.all()
        return context
Esmail Shabayek
  • 348
  • 3
  • 11