Question

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 :)

Was it helpful?

Solution

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')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top