|
10 | 10 | from django.db.models import Q |
11 | 11 | from DjangoEcommerce.settings import BASE_URL |
12 | 12 | from django.views.decorators.csrf import csrf_exempt |
| 13 | +from .filters import ProductFilter |
| 14 | +from django.db.models import Prefetch |
13 | 15 |
|
14 | 16 | @login_required(login_url="/admin/") |
15 | 17 | def admin_home(request): |
@@ -194,7 +196,6 @@ def get(self,request,*args,**kwargs): |
194 | 196 | categories_list.append({"category":category,"sub_category":sub_category}) |
195 | 197 |
|
196 | 198 | merchant_users=MerchantUser.objects.filter(auth_user_id__is_active=True) |
197 | | - |
198 | 199 | return render(request,"admin_templates/product_create.html",{"categories":categories_list,"merchant_users":merchant_users}) |
199 | 200 |
|
200 | 201 | def post(self,request,*args,**kwargs): |
@@ -264,28 +265,28 @@ class ProductListView(ListView): |
264 | 265 | paginate_by=3 |
265 | 266 |
|
266 | 267 | def get_queryset(self): |
267 | | - filter_val=self.request.GET.get("filter","") |
| 268 | + self.filterset = ProductFilter(self.request.GET, queryset=super().get_queryset()) |
268 | 269 | order_by=self.request.GET.get("orderby","id") |
269 | | - if filter_val!="": |
270 | | - products=Products.objects.filter(Q(product_name__contains=filter_val) | Q(product_description__contains=filter_val)).order_by(order_by) |
271 | | - else: |
272 | | - products=Products.objects.all().order_by(order_by) |
273 | | - |
274 | | - product_list=[] |
275 | | - for product in products: |
276 | | - product_media=ProductMedia.objects.filter(product_id=product.id,media_type=1,is_active=1).first() |
| 270 | + products=self.filterset.qs.order_by(order_by) |
| 271 | + media_qs=ProductMedia.objects.filter(media_type=1, is_active=1) |
| 272 | + prefetched_products=products.prefetch_related( |
| 273 | + Prefetch('productmedia_set', queryset=media_qs, to_attr='filtered_media') |
| 274 | + ) |
| 275 | + |
| 276 | + product_list = [] |
| 277 | + for product in prefetched_products: |
| 278 | + product_media=product.filtered_media[0] if product.filtered_media else None |
277 | 279 | product_list.append({"product":product,"media":product_media}) |
278 | 280 |
|
279 | 281 | return product_list |
280 | 282 |
|
281 | 283 | def get_context_data(self,**kwargs): |
282 | 284 | context=super(ProductListView,self).get_context_data(**kwargs) |
283 | | - context["filter"]=self.request.GET.get("filter","") |
284 | 285 | context["orderby"]=self.request.GET.get("orderby","id") |
285 | 286 | context["all_table_fields"]=Products._meta.get_fields() |
| 287 | + context["all_subCategories"]=SubCategories.objects.all() |
286 | 288 | return context |
287 | 289 |
|
288 | | - |
289 | 290 | class ProductEdit(View): |
290 | 291 |
|
291 | 292 | def get(self,request,*args,**kwargs): |
|
0 commit comments