Django - 可怕的“非序列迭代”
您好,我希望根据俱乐部的来源来填充会员列表。
这是我的代码:
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)
Hi I'm looking to populate a list of members, based on where their club comes from.
This is my code:
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)
However, when evaluating for m in members_list:
it throws an 'iteration over non-sequence'
I'm not entirely sure why? Can anyone give me any ideas?!
EDIT:
Solved using the following:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非查看会员模型,否则无法发表评论。但是
clubs = Club.objects.filter(location__country__in = list_of_user_countries)
如果您的最终列表是成员列表,您可以按照我上面提到的方式执行此操作(至少以优化的方式)
Can't comment unless looking at Member model. But
clubs = Club.objects.filter(location__country__in = list_of_user_countries)
If your final list is list of members, you can do that as I mentioned above (at least in optimized way)