Wednesday, June 30, 2021

Sorting price and discount price (urgent)

I am facing a problem in including the discount_price into consideration when sorting price through the products. Kindly help me to do so. The below attached files are filters.py, views.py and  models.py.

Regards,
Aritra

class ProductFilter(django_filters.FilterSet):
    sort = OrderingFilter(
        choices=(
            ('price''Lowest to Highest'),
            ('-price''Highest to Lowest'),
            ),
        fields={
            'price''price',
            'price':'discount_price'
        },
    )
    price = RangeFilter()
    class Meta:
        model = Items
        fields = ['size''category']

class Product(ListView):
    model = Items
    paginate_by = 6
    template_name = 'products.html'
    ordering = ["-id"]

    def get_queryset(self):
        queryset = super().get_queryset()
        filter = ProductFilter(self.request.GETqueryset)
        return filter.qs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        queryset = self.get_queryset()
        filter = ProductFilter(self.request.GETqueryset)
        context["filter"= filter
        return context
class Items(BaseModel):
    title = models.CharField(max_length=100, null=True, blank=True)
    price = models.FloatField(null=True, blank=True)
    discount_price = models.FloatField(max_length=100, blank=True, null=True)
    description = models.TextField(max_length=500)
    size = models.CharField(choices=SIZES, default=SIZES[0][0], max_length=10)
    category = models.CharField(choices=CATEGORY, default=CATEGORY[0][0], max_length=1)
    featured = models.BooleanField(default=False)
    availability = models.CharField(choices=AVAILABILITY, default=AVAILABILITY[0][0], max_length=1)
    image = ResizedImageField(upload_to="", null=True, blank=True)
    slug = models.SlugField(max_length=100)
    date_added = models.DateField(default=timezone.now)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFecadsUerZLP7gj7M23DLjytHQcuWc1ys2nqEcs_JNLKMc_Qg%40mail.gmail.com.

No comments:

Post a Comment