简体   繁体   中英

django : concatenate 2 queries or special order?

I have to make such query :

similar_brokers_ = Profile.objects.filter(Q(tp=broker.tp) |  Q(md=broker.md)).exclude(Q(id=broker.id))[:4]

i need to get 4 similar brokers, which have the same tp field or md field

so this query does it, but i need brokers with the same tp field go first, after them should go brokers with the same md field.

how can i do it better?

for example :

broker1  with tp=1, md=100
broker2  with tp=2, md=100
broker3  with tp=1, md=99

get brokers similat to broker1 : first should go broker3 (with tp =1) and after broker2 (with md =100)

Did you try with order_by('tp', 'md') ? See docs for order_by

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM