Sorting price and discount price (urgent)

72 views
Skip to first unread message

Aritra Ray

unread,
Jul 1, 2021, 2:04:48 AM7/1/21
to django...@googlegroups.com
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)

Kasper Laudrup

unread,
Jul 1, 2021, 4:01:12 AM7/1/21
to django...@googlegroups.com
On 7/1/21 8:03 AM, Aritra Ray wrote:
> 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.
>

Instead of writing "urgent" in hope it will increase your chances of
getting help faster, you'll have a much better chance of getting some
useful help if you spend some time describing the exact problem you have
instead of just "a problem".

This might help you:

https://betterprogramming.pub/how-to-ask-questions-about-programming-dcd948fcd2b

Kind regards,

Kasper Laudrup

Aritra Ray

unread,
Jul 1, 2021, 4:04:23 AM7/1/21
to django...@googlegroups.com
I tried to describe the problem but till my capability and it is urgent, not just for increasing the possibility of getting help. Thank you for your advice and help.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0c454b7d-cb51-ea95-d0e8-ca4be0c2bad7%40stacktrace.dk.

Shailesh Yadav

unread,
Jul 1, 2021, 6:36:31 AM7/1/21
to django...@googlegroups.com
yes, I agree with Kasper please don't use urgent  OR very urgent, and please describe your questions in detail.
from where you getting this discount_price and why you using the price key for two different values. (can you check by printing discount_price)


    fields={
            'price''price',
            'price':'discount_price'
        },



Thanks & Regards
Shailesh Yadav



Aritra Ray

unread,
Jul 1, 2021, 8:28:13 AM7/1/21
to django...@googlegroups.com
Hi,

I will avoid using 'urgent'. The problem is as described above, I want to sort my products in ascending or descending price order which will include the discount price too if it exists. Right now, if there are two products, A and B; A being ₹900 and B being ₹1200 too but after discount, ₹ 800, the sorting order highest to lowest price is: B,A instead of A,B. As Shailesh asked, discount_price is a model field as I've given in the code about. And the 'fields = {'price': 'discount_price'}' is wrong after I searched a bit on the internet, kindly ignore that. 

Kindly suggest a way to include discount_price in the sorting parameter where it'll be considered instead of the original price itself. 

SKYLINE TV

unread,
Jul 1, 2021, 5:28:11 PM7/1/21
to Django users
the word urgen is bad, do lot of google boss
Reply all
Reply to author
Forward
0 new messages