Django 模型过滤器

发布于 2025-01-08 07:18:34 字数 697 浏览 0 评论 0原文

我花了过去几个小时查看 Django Docs 和类似的问题,但我仍然不清楚如何解决这个问题......

本质上我想访问与某个组相关的电子邮件地址列表,所以我可以向属于该组的这些客户(名为 group_one、group_two 等)发送电子邮件,

class Group(models.Model):
  name = models.CharField(primary_key=True)
  mailing_list = models.ManyToManyField("Customer", null=True)  

class Customer(models.Model):
  name = models.CharField()
  email = models.EmailField(primary_key=True)

我已经尝试过

group_mail_list = Group.objects.filter(name=group_two)

这会返回 group_two 的查询对象,但是当我尝试执行 __contains 过滤器时,我收到错误:

TypeError: Related Field has invalid lookup: contains

任何人都可以帮忙我出去吗?不确定是否是因为它是多对多关系?

提前致谢 :)

I've spent the last few hours looking at Django Docs and similar questions on here but I'm still unclear on how to tackle the problem...

In essense I want to access the list of email addresses relevent to a certain group, so I can send an email to these customers who are part of this group (named group_one, group_two etc)

class Group(models.Model):
  name = models.CharField(primary_key=True)
  mailing_list = models.ManyToManyField("Customer", null=True)  

class Customer(models.Model):
  name = models.CharField()
  email = models.EmailField(primary_key=True)

I've tried

group_mail_list = Group.objects.filter(name=group_two)

And this returns an Query Object for group_two but when I try to do a __contains filter I get a error:

TypeError: Related Field has invalid lookup: contains

Anyone help me out? Not sure if it's because it's a many-to-many relationship perhaps?

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦幻的心爱 2025-01-15 07:18:34

那么,您真正想要的是客户。从表面上看,这似乎违反直觉,因为您想要Group的电子邮件,但该字段位于Customer而不是Group

Customer.objects.filter(group__name='group_two').values_list('email')

What you really want is Customers, then. It seems counter-intuitive on the surface, because you're wanting emails for a Group, but that field is on Customer not Group:

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