如何使用django ORM显示多个对象中的多个对象?

vd2z7a6w  于 2023-02-25  发布在  Go
关注(0)|答案(1)|浏览(129)
if co is None:
    try:
      address = **Contract**.objects.filter(address= servicesearch)
      print(address)
    except Contract.DoesNotExist:
      address= None

    for add in address:
      co = add.contractservices_set.all()
      print(co)

第一个查询产生查询集[合同:合同对象(52)〉、〈合同:合同标的(55)〉]〉
第二个查询产生查询集[合同服务:订约承办事务对象(88)〉,〈订约承办事务:订约承办事务对象(89)〉,〈订约承办事务:订约承办事务对象(90)〉,〈订约承办事务:合同服务对象(91)〉]〉〈查询集[〈合同服务:订约承办事务对象(98)〉,〈订约承办事务:订约承办事务对象(101)〉、〈订约承办事务:订约承办事务对象(102)〉,〈订约承办事务:订约承办事务对象(99)〉,〈订约承办事务:订约承办事务对象(103)〉,〈订约承办事务:合同服务对象(100)〉,〈合同服务:订约承办事务对象(104)〉]〉
一切顺利......
然而,当我试图呈现结果时,它只显示55的合同对象。我想同时显示52和55的合同对象。请帮助...
我的分页器视图是..

paginator = Paginator(co,8)
    if request.GET.get('page') == None:
        page_number = 1
    else:
        page_number =request.GET.get('page')
      
    DataFinal=paginator.get_page(page_number)
    totalpage =DataFinal.paginator.num_pages

我不能更改模板,因为它只是众多搜索条件中的一个。。your text

xfb7svmp

xfb7svmp1#

您在分页结果中只设置了co
您只需设置一个查询,即可获取与ContractServices一起附加的Contract:

address = Contract.objects.filter(address=servicesearch)
address = address.prefetch_related("contractservices_set")

...

paginator = Paginator(address, 8)
if request.GET.get('page') == None:
    page_number = 1
else:
    page_number =request.GET.get('page')
      
DataFinal = paginator.get_page(page_number)

...
# in the html
{% for address in DataFinal %}
    {% for service in address.contractservices_set.all %}
        {{service.XXX}}
    {% endfor %}
{% endfor %}

相关问题