Django 模型过滤器
我花了过去几个小时查看 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您真正想要的是
客户
。从表面上看,这似乎违反直觉,因为您想要Group
的电子邮件,但该字段位于Customer
而不是Group
:What you really want is
Customer
s, then. It seems counter-intuitive on the surface, because you're wanting emails for aGroup
, but that field is onCustomer
notGroup
: