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

查看:38
本文介绍了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?

推荐答案

是否有可能因为您将原始字符串传递给查询集的格式不正确,请尝试将字符串转换为日期时间对象.

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天全站免登陆