Django - 可怕的“非序列迭代”

发布于 2024-09-16 02:10:27 字数 791 浏览 6 评论 0原文

您好,我希望根据俱乐部的来源来填充会员列表。

这是我的代码:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2024-09-23 02:10:27

除非查看会员模型,否则无法发表评论。但是

  1. 我们不能使用 .filter 进行后退导航,而不是 get_members
  2. 我们需要那么多循环以及循环内的数据库访问吗?例如:

clubs = Club.objects.filter(location__country__in = list_of_user_countries)

如果您的最终列表是成员列表,您可以按照我上面提到的方式执行此操作(至少以优化的方式)

Can't comment unless looking at Member model. But

  1. Can't we use .filter with back navigation, instead of get_members
  2. Do we need those many loops, and db access inside loop? ex:

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文