我有一个模型,如下所示:
class Loves(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
user_name = models.CharField(max_length=200)模型中的数据如下所示:
轮询choice_text用户
1个麦克风
1b麦克风
1c詹姆斯
2个詹姆斯
2b旋律
一维麦克风
如何输出如下所示的列表:
用户元组
mike (1,a),(1,b),(1,d)
james (1,c),(2,a)
旋律(2,b)
谢谢。
发布于 2012-09-28 05:59:22
collection={}
for l in Loves.objects.all():
if l.user_name in collection:
collection[l.user_name].append((l.poll,l.choice_text))
else:
collection[l.user_name]=[(l.poll,l.choice_text)]
# and to turn it into a list just do
results_list=list(collection)https://stackoverflow.com/questions/12609626
复制相似问题