python - Django - 可怕的 'iteration over non-sequence'

标签 python django django-models django-queryset

您好,我正在根据俱乐部的来源来填充成员(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)模型,否则无法发表评论。但是

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

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

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

关于python - Django - 可怕的 'iteration over non-sequence',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3565166/

相关文章:

python - HTTP 000 连接失败

python - 使用 tf slim 重新训练预训练的 ResNet-50 模型以进行分类

python - 如何使用 python zipfile 库检查 zip 文件是否拆分为多个存档?

python - matplotlib 中两个定义函数之间的区域孵化

python - Django 按字段对结果进行分组

javascript - Django 中的电子邮件确认实现

mysql - 仅显示 django 模型管理中其他模型中的唯一记录

python - 在 Django 模板中按索引引用列表项?

python - Django FileField with upload_to 在运行时确定

python - Django图像字段: full or relative path?