Django concatenate two querysets for same model

前端 未结 2 792
[愿得一人]
[愿得一人] 2021-01-20 21:28

I have a list of Products, each belonging to a different Distributor.

I need to display a form for each of those products and their corresp

相关标签:
2条回答
  • 2021-01-20 21:36

    You can try to do it like here:

    https://stackoverflow.com/a/2176471/4971083

    It's the solution for ordering your records by specific value in Django. You could order your records by distributor_name = 'FirstDistributor'

    p= Product.objects.filter(product__id=product_id).extra(
    select={'is_top': " distributor__name='FirstDistributor'"})
    p = p.extra(order_by = ['-is_top'])
    
    0 讨论(0)
  • 2021-01-20 21:47

    You can use itertools to combine the two:

    from itertools import chain
    result_list = list(chain(page_list, article_list, post_list))
    

    Source: https://stackoverflow.com/a/434755/3279262

    0 讨论(0)
提交回复
热议问题