我在view中调用model,我的两个model是一对多的关系,我希望能把A和B表中的记录查出来转成list格式,再遍历A的list,然后将B与之匹配的记录拼接起来
我在view中调用model,我的两个model是一对多的关系,我希望能把A和B表中的记录查出来转成list格式,再遍历A的list,然后将B与之匹配的记录拼接起来
简化的
def A(Model):
title=field()
other1=field()
other2=field()
def B(Model):
a=field(A)
b=field()
c=field()
query_list = B.object.value_list('b','c','a__title','a__other1','a__other2')
query_list就是一个list,返回的结果是元组(id,b,c,a__title,a__other1,a__other2),
也可以用values代替value_list返回的是[{'id':1,'b':'11'..},{'id':2,'b':'22'..}]
4 回答4.4k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
1 回答3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
1 回答4.5k 阅读✓ 已解决
1 回答3.8k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
perhaps you can use select with related and then convert the results to list