0
category = request.GET.get('cat')
from_p = request.GET.get('from')
to_p = request.GET.get('to')

q = Q()
if category:
    q |= Q(category=category)
if from_p:
    q |= Q(price__gt=from_p)
if to_p:
    q |= Q(price__lt=to_p)

s = Sample.objects.filter(q)

www.example.com/?cat=0&from=300&to=600

如果第一个是category- from_p并且to_p标准不起作用。如何解决?它仅适用于一个标准。如果用户设置(例如)category并按from_p这些标准搜索,我需要。

4

1 回答 1

0

EDITED

Here it is:

q_cat = Q(category=category) if category else Q()
q_from = Q(price__gt=from_p) if from_p else Q()
q_to = Q(price__lt=to_p) if to_p else Q()

q = q_cat | ( q_from  & q_to )

s = Sample.objects.filter(  q )
于 2013-03-01T15:09:15.470 回答