如何获取给定的zipcodes列表的事件?

时间:2013-11-29 08:55:13

标签: django python-2.7 django-models

您好我有zipcodes列表

zipcodes = ['1234', '2132', '12312', '123123', '12317']

我创建了一个事件模型如下:

class Event(models.Model):
 title = models.CharField(max_length=40)
 description = models.CharFi
 address = models.TextField(max_length=350, null=True, blank=True)
 city = models.CharField(max_length=50, blank=True, null=True)
 state = models.CharField(max_length=50, blank=True, null=False)
 province = models.CharField(max_length=50, blank=True, null=False)
 zipcode = models.CharField(max_length=10, blank=True)

现在对于给定的zipcodes列表,我需要获取所有事件对象。如何在django中为此编写查询?

1 个答案:

答案 0 :(得分:2)

使用in

zipcodes = ['1234', '2132', '12312', '123123', '12317']
Event.objects.filter(zipcode__in=zipcodes)