im trying to make endless pagination with filtered data and im geting the data with get request.
views.py:
class ProductsView(ListView):
paginate_by = 20
context_object_name = 'products'
template_name = "urunler.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
renkler = Renk.objects.all()
beden = Varyasyon.objects.all()
katagori = Grub.objects.all()
order = self.request.GET.get('order')
filtered = Stok.objects.all().order_by('-urun_id__kayit_tarihi','foto').distinct('urun_id__kayit_tarihi','foto')
checked_var = self.request.GET.getlist('varyasyon_id__in')
checked_renk = self.request.GET.getlist('renk_id__in')
checked_kat = self.request.GET.getlist('grub_id__in')
if self.request.GET.getlist("varyasyon_id__in"):
filtered = filtered.filter(
Q(varyasyon_id__in = self.request.GET.getlist("varyasyon_id__in"))
)
if self.request.GET.getlist("renk_id__in"):
filtered = filtered.filter(
Q(renk_id__in = self.request.GET.getlist("renk_id__in"))
)
if self.request.GET.getlist("grub_id__in"):
filtered = filtered.filter(
Q(grub_id__in = self.request.GET.getlist("grub_id__in"))
)
if order == "date":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0)
)
elif order == "sale":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0) & Q(indirim = True)
)
elif order == "tesettur":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0) & Q(urun_id__tesettur = True)
)
else:
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0)
)
context = {"renkler":renkler,"bedenler":beden,"katagoriler":katagori,'filtered':filters,'checkedvar':checked_var,'checkedrenk':checked_renk,'checkedkat':checked_kat}
self.queryset = context
return context
queryset = get_context_data()
i want to set the queryset from get_context_data's return but it asks for self argument.
error
TypeError: ProductsView.get_context_data() missing 1 required positional argument: 'self'
when i define queryset on the top it says that get_context_data is not defined.
CodePudding user response:
try to override get_queryset()
like this and then override your context using get_context_data
i tried it and it worked for me.
class ProductsView(ListView):
paginate_by = 12
context_object_name = 'filtered'
template_name = 'urunler.html'
def get_queryset(self):
order = self.request.GET.get('order')
filtered = Stok.objects.all().order_by('-urun_id__kayit_tarihi','foto').distinct('urun_id__kayit_tarihi','foto')
checked_var = self.request.GET.getlist('varyasyon_id__in')
checked_renk = self.request.GET.getlist('renk_id__in')
checked_kat = self.request.GET.getlist('grub_id__in')
if self.request.GET.getlist("varyasyon_id__in"):
filtered = filtered.filter(
Q(varyasyon_id__in = self.request.GET.getlist("varyasyon_id__in"))
)
if self.request.GET.getlist("renk_id__in"):
filtered = filtered.filter(
Q(renk_id__in = self.request.GET.getlist("renk_id__in"))
)
if self.request.GET.getlist("grub_id__in"):
filtered = filtered.filter(
Q(grub_id__in = self.request.GET.getlist("grub_id__in"))
)
if order == "date":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0)
)
elif order == "sale":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0) & Q(indirim = True)
)
elif order == "tesettur":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0) & Q(urun_id__tesettur = True)
)
else:
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0)
)
return filters
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
renkler = Renk.objects.all()
beden = Varyasyon.objects.all()
katagori = Grub.objects.all()
order = self.request.GET.get('order')
filtered = Stok.objects.all().order_by('-urun_id__kayit_tarihi','foto').distinct('urun_id__kayit_tarihi','foto')
checked_var = self.request.GET.getlist('varyasyon_id__in')
checked_renk = self.request.GET.getlist('renk_id__in')
checked_kat = self.request.GET.getlist('grub_id__in')
if self.request.GET.getlist("varyasyon_id__in"):
filtered = filtered.filter(
Q(varyasyon_id__in = self.request.GET.getlist("varyasyon_id__in"))
)
if self.request.GET.getlist("renk_id__in"):
filtered = filtered.filter(
Q(renk_id__in = self.request.GET.getlist("renk_id__in"))
)
if self.request.GET.getlist("grub_id__in"):
filtered = filtered.filter(
Q(grub_id__in = self.request.GET.getlist("grub_id__in"))
)
if order == "date":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0)
)
elif order == "sale":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0) & Q(indirim = True)
)
elif order == "tesettur":
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0) & Q(urun_id__tesettur = True)
)
else:
filters = filtered.filter(
Q(urun_id__yayin = True) & Q(stok_adet__gt = 0)
)
context.update({"renkler":renkler,"bedenler":beden,"katagoriler":katagori,'checkedvar':checked_var,'checkedrenk':checked_renk,'checkedkat':checked_kat})
return context
CodePudding user response:
Maybe you should override get_queryset
method. A queryset
is not a context
dict:
class ProductsView(ListView):
paginate_by = 20
context_object_name = 'products'
template_name = "urunler.html"
def get_queryset(self):
queryset = super().get_queryset()
# do stuff here to change queryset dynamically
...
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# do stuff here to update your context dict
...
return context
CodePudding user response:
First - why your code fails. You override get_context_data
, then make invalid call to it (you don't pass self
to it at all), and finally all this happens during class creation, not instantiation, so get_context_data
isn't aware about request when you call it on last line. And context_data
is not same thing as queryset
. Here's what you can do instead to get paginated content of filtered
queryset:
class ProductsView(ListView):
paginate_by = 20
context_object_name = 'filtered'
template_name = 'urunler.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'renkler': Renk.objects.all(),
'bedenler': Varyasyon.objects.all(),
'katagoriler': Grub.objects.all(),
'checkedvar': self.request.GET.getlist('varyasyon_id__in'),
'checkedrenk': self.request.GET.getlist('renk_id__in'),
'checkedkat': self.request.GET.getlist('grub_id__in'),
})
return context
def get_queryset(self):
filtered = Stok.objects.all().order_by('-urun_id__kayit_tarihi','foto').distinct('urun_id__kayit_tarihi','foto')
checked_var = self.request.GET.getlist('varyasyon_id__in')
checked_renk = self.request.GET.getlist('renk_id__in')
checked_kat = self.request.GET.getlist('grub_id__in')
if checked_var:
filtered = filtered.filter(varyasyon_id__in=checked_var)
if checked_renk:
filtered = filtered.filter(renk_id__in=checked_renk)
if checked_cat:
filtered = filtered.filter(grub_id__in=checked_cat)
filtered = filtered.filter(urun_id__yayin=True, stok_adet__gt=0)
order = self.request.GET.get('order')
if order == "sale":
filtered = filtered.filter(indirim=True)
elif order == "tesettur":
filtered = filtered.filter(urun_id__tesettur=True)
return filtered