我有一个django查询来过滤模型并打印出符合搜索条件。
以下是观点
if user_search != '' and (start_date != '' and end_date != ''):
items = Order.objects.filter(
Q(client__email__iexact=user_search) , Q(created_on__range=(start_date, end_date))
)
print "items ", items
django过滤器用于打印用户搜索的电子邮件地址,该地址属于日期范围。 但是代码我还打印了一些未经搜索但仍在日期范围内的电子邮件
这是模型
class Order(models.Model):
client = models.ForeignKey(User, related_name = "order_user")
order_number = models.CharField(max_length = 12)null=True)
terms_and_conditions = models.BooleanField(default=False)
order_status = models.CharField(max_length = 20, choices = ORDER_STATUS, default = "new")
created_on = models.DateTimeField(auto_now_add = True)
edited_on = models.DateTimeField(auto_now_add = False)
def __unicode__(self):
return self.order_number
答案 0 :(得分:1)
使用&
对Q
个对象执行AND操作。
if user_search != '' and (start_date != '' and end_date != ''):
items = Order.objects.filter(
Q(client__email__iexact=user_search) & Q(created_on__range=(start_date, end_date))
)
print "items ", items