我试图将两个查询返回数据合并为一个对象,例如
viewAllUq = Uqmain.objects.raw('''
select *, uqmain.id as uqmainID, company.name as companyName, uqmain.created_by as uqMainCreated_by,
company.slug as companySlug, uqmain.slug as uqmainSlug from uqmain
Left join company ON company.id = uqmain.company_id
Left join user ON user.id = uqmain.created_by
where uqmain.deleted = 0''')
doneBids = Uqbids.objects.raw('''
select DISTINCT uq_main_id as bidDoneUqmain, id from
uqbids where created_by = %s''',[request.user.id])
这里 viewAllUq 和 doneBids 是两个原始查询,我想将此查询合并为一个查询,例如
merged = viewAllUq + doneBids #I want something like this to merge them
for data in merged:
print(data.companyName) # Access viewAllUq object data
print(data.bidDoneUqmain) # Access doneBids object data
我做了一些Google搜索,但没有帮助我。
答案 0 :(得分:1)
使用dict进行数据库查询可能会更好。像这样:
merged_dict ={"viewAllUq":viewAllUq,
"doneBids": doneBids
}
view_all_uq = merged_dict.get("viewAllUq")
如果需要,您可以按字典进行迭代。