您好,我正在根据俱乐部的来源来填充成员(member)列表。
这是我的代码:
members = []
if userprofile.countries.count() > 0:
for c in userprofile.countries.all():
clubs = Club.objects.filter(location__country = c)
for club in clubs:
members_list = Member.objects.get_members(club)
for m in members_list:
members.append(m)
但是,在评估 for m in members_list:
时它抛出“非序列迭代”
我不太确定为什么?谁能给我任何想法吗?!
编辑:
使用以下方法解决:
members = []
if userprofile.countries.count() > 0:
members_list = member.objects.filter(memberstoentities__club__location__country__in = userprofile.countries.all())
for m in members_list:
members.append(m)
最佳答案
除非查看成员(member)模型,否则无法发表评论。但是
- 我们不能使用 .filter 进行后退导航,而不是 get_members
- 我们需要那么多循环以及循环内的数据库访问吗?例如:
clubs = Club.objects.filter(location__country__in = list_of_user_countries)
如果您的最终列表是成员列表,您可以按照我上面提到的方式执行此操作(至少以优化的方式)
关于python - Django - 可怕的 'iteration over non-sequence',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3565166/