我想在列表中添加区域,包含该国家/地区,区域和国家/地区模型之间的M2M关系。我不能得到国家代码的属性,它说“结肠预期”。 如果条件错过了什么?
views.py
notification = Notification.objects.filter(**condition). \
exclude(notification_user__in=users). \
order_by('notificationType__priority', '-start_date')
notifications = []
for n in notification:
print len(n.region.all())
if len(n.region.all())==0:
notifications.append(n)
else:
if (region.countries__in=country):
notifications.append(n)
Notififcation models.py
class Notification(models.Model):
title = models.CharField(max_length=75)
description = models.TextField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()
application = models.ManyToManyField('Products.Application')
notificationType = models.ForeignKey(NotificationType)
url = models.URLField(null=True, blank=True)
region = models.ManyToManyField('Geolocations.Region', null=True, blank=True)
image = models.ImageField(null=True, blank=True)
user = models.ManyToManyField(User, through='Notification_User')
Geolocations models.py
class Country(models.Model):
code=models.CharField(max_length=2)
name=models.CharField(max_length=36)
def __unicode__(self):
return u'%s - %s' % (self.code, self.name)
class Meta:
verbose_name_plural="Countries"
class Region(models.Model):
name=models.CharField(max_length=10)
countries=models.ManyToManyField(Country)
答案 0 :(得分:0)
region.countries__in = country是一个赋值,因为它是在if语句的条件下执行的,所以你得到“冒号预期”。
如果您想检查与某个地区相关的国家/地区,请执行以下操作:
# country is the country's code
n.region.filter(countries_code=country).exists()