0

I have a query

 messages = Message.objects.all()

I want to create a Create a comma separated string of all numbers from message.number

This is what I have tried:

number = ""
for obj in messages:
     number = number + "," + obj.number

How can I do this? PS this string can be VERY long over 400,000 numbers!

4

2 回答 2

8

使用join()

",".join(str(msg.number) for msg in Message.objects.all())

此外,由于您有一个很大的查询集,请考虑使用查询集迭代器将结果分成块。有关更多信息,请参阅:

于 2013-08-27T11:28:41.227 回答
1

使用str.join

number = ','.join(messages)
于 2013-08-27T11:28:12.650 回答