Reputation: 259
The title might look a little confusing. However, what I am trying to do is quite simple. I have the following models:
class ListEntry(models.Model):
name = models.CharField(max_length=64)
expire_date = models.DateField('date of expiration')
create_date = models.DateField('date created')
class ListWarning(models.Model):
mailing_list = models.ForeignKey(ListEntry)
first_warning = models.BooleanField()
last_warning = models.BooleanField()
I want to query for a List Warning object that references a mailing list with a certain name. Here's an example:
first_warning_list = ListWarning.objects.filter(mailing_list.name='PH_212', first_warning=True, last_warning=False)
However, for some reason, python is complaining of a syntax error:
first_warning_list = ListWarning.objects.filter(mailing_list.name='PH_212', first_warning=True, last_warning=False)
SyntaxError: keyword can't be an expression
What is the syntactically correct way to do this query?
Upvotes: 0
Views: 19
Reputation: 8539
Use double underscores rather than dot notation to refer to mailing_list name
first_warning_list = ListWarning.objects.filter(mailing_list__name='PH_212', first_warning=True, last_warning=False)
Docs here.
Django offers a powerful and intuitive way to “follow” relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes. To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.
Upvotes: 2