django日期过滤器gte和lte [英] django date filter gte and lte

查看:1125
本文介绍了django日期过滤器gte和lte的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一组特定参数内查找数据 我正在建立一个小型的预订系统,该系统使用户可以查看哪些小汽车可以用于他们的野生动物园旅行.

I need to find data within a certain set of parameters I am building a small booking system, that lets user see what vehicles are available for booking for their little safari trip.

系统具有先前输入或由客户先前进行的预订.

The system has bookings that have been entered previously or made previously by a client.

如果预订的pickup_date = 2011-03-01dropoff_date = 2011-03-15并且我在以下视图中使用pickup=2011-03-09dropoff=2011-03-14运行查询,则不会返回任何结果以查看是否在该时间范围内进行了预订

If a booking's pickup_date = 2011-03-01 and dropoff_date = 2011-03-15 and I run a query with pickup=2011-03-09 and dropoff=2011-03-14 in my views as below, it doesn't return any results to see if a booking within that timeframe has been made.

views.py

def dates(request, template_name='groups/_dates.html'):
    pickup=request.GET.get('pickup','None');
    dropoff=request.GET.get('dropoff','None');
    order = Order.objects.filter(pick_up__lte=pickup).filter(drop_off__gte=dropoff)

    context = {'order':order,}

    return render_to_response(template_name,context,
        context_instance=RequestContext(request))

有关如何执行此操作的任何建议? 还是我应该考虑运行此查询的另一种方式?

Any suggestions on how to do this? Or should I be looking at an alternate way of running this query?

推荐答案

是否有可能当您将原始字符串传递给queryset的格式不正确时,尝试将字符串转换为datetime对象.

Could it be posible that as your passing the raw string to the queryset is not on the correct format, try converting the strings to datetime objects.

稍后,您可以尝试使用范围查找在某些数据库引擎上更高效,并且更易于阅读和编写代码.

Later you can try using the range lookup is more efficient on some DB engines and more simple to read and code.

from django.db.models import Q

start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
orders = Order.objects.filter(drop_off__gte=start_date, pick_up__lte=end_date)
# Or maybe better
orders = Order.objects.filter(Q(drop_off__gte=start_date), Q(pick_up__lte=end_date))

这篇关于django日期过滤器gte和lte的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆