how to display products order by *Price* in category page?

1,778 views
Skip to first unread message

TIM CHOU

unread,
Jan 19, 2014, 4:51:48 AM1/19/14
to django...@googlegroups.com
hi

In category page,I want to display all products sorted by prices,as we know,in Oscar,price field is not in product table,so it seems kind of complex,how to do that? 

ps:what about order by  "sold quantity"?

thanks!

Izidor Matušov

unread,
Jan 20, 2014, 5:00:15 AM1/20/14
to django...@googlegroups.com
Hi,

there is a reason why sorting products by price is hard to do in Oscar.
Since Oscar 0.6 you can specify very complex logic behind price. [0]
E.g. you can set the price based on the country, time of the day.

If you don't mind duplicating the logic, you can the following thing:

<code>
# Feel free to filter stockrecords based on what you want,
# e.g. country, partner, time of day
stockrecords = StockRecord.objects.all()

stock_products = stockrecords.order_by('-price_excl_tax').\
.select_related('product')
</code>

Izidor

0:
http://django-oscar.readthedocs.org/en/latest/topics/prices_and_availability.html
> --
> https://github.com/tangentlabs/django-oscar
> http://django-oscar.readthedocs.org/en/latest/
> https://twitter.com/django_oscar
> ---
> You received this message because you are subscribed to the Google
> Groups "django-oscar" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-oscar...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-oscar.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-oscar/c0d0d922-e788-4e90-a3fd-79b63387d1b5%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

David Winterbottom

unread,
Jan 20, 2014, 5:24:21 AM1/20/14
to django-oscar
Things can be simpler if your site doesn't use multiple stockrecords per product.  If there's a one-to-one relationship, then you could do something like:

Product.objects.all().order_by('stockrecords__price_excl_tax')

A better (but more involved) solution is to use a search backend via Haystack.  When you build the index, you can include a price field calculated appropriately for your domain.  See, for example:


--
https://github.com/tangentlabs/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
--- You received this message because you are subscribed to the Google Groups "django-oscar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-oscar+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
David Winterbottom
Technical Director

Tangent Labs
84-86 Great Portland Street
London W1W 7NR
England, UK

TIM CHOU

unread,
Jan 20, 2014, 9:52:07 AM1/20/14
to django...@googlegroups.com, david.win...@tangentlabs.co.uk
Great thanks! It works.

在 2014年1月20日星期一UTC+8下午6时24分21秒,David Winterbottom写道:

--
https://github.com/tangentlabs/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
--- You received this message because you are subscribed to the Google Groups "django-oscar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-oscar...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages