I'm not really experienced in Django, but now I am trying to develop simple project with model class like this:
我对Django不是很在行,但是现在我正在尝试用model类开发一个简单的项目:
class FacebookEvent(models.Model):
start_time = models.DateTimeField(null=True)
end_time = models.DateTimeField(null=True)
In view file - there's no problem when I want to get all objects where start_time is today, tomorrow or in any other date range (including hours):
在查看文件中—当我想获得start_time今天、明天或任何其他日期范围(包括小时)的所有对象时,没有问题:
my_date= request.POST.get('my_date','') # for eg. 2015-10-26
events = FacebookEvent.objects.all().filter(start_time__range=
(datetime.combine(my_date,(datetime.min).time()), datetime.combine(my_date,(datetime.max).time()))).order_by('start_time')
but now i have to check if requested date (my_date) is between start_time and end_time from model. I just have to do the same what I was doing before, but a little bit inversely...
但是现在我必须检查请求日期(my_date)是否在start_time和end_time之间。我要做的和以前一样,但是有点反……
It took me few hours, but still I can't find any concept how to solve this problem. Anybody have any idea?
我花了几个小时的时间,还是找不到解决这个问题的方法。有人知道吗?
Thanks in advance.
提前谢谢。
4 个解决方案
#1
1
Im not sure if I understand the question.
我不确定我是否理解这个问题。
If you have a model instance and a date:
如果您有一个模型实例和一个日期:
First you need to convert your date to a datetime.datetime object
首先需要将日期转换为datetime。datetime对象
from dateutil.parser import parse
my_date= request.POST.get('my_date','')
mdate = parse(mydate)
Then you can use mdate to do logical comparisons. i.e: mdate > fbEvent.start_time
然后可以使用mdate进行逻辑比较。我。艾凡:mdate > fbEvent.start_time
If you want model instances between dates
如果需要在日期之间建立模型实例
mdate = parse(mydate)
events = FacebookEvent.objects.filter(start_time__gt=mdate, end_time__lt=mdate)
#2
0
The following should help you get started:
以下内容可以帮助你开始:
# assuming Sample is your model class and sampledate is your column to filter on
samples = Sample.objects.filter(sampledate__gt=datetime.date(2011, 1, 1), sampledate__lt=datetime.date(2011, 1, 31))
#3
0
you can do like:
你能做的:
from datetime import datetime
my_date= request.POST.get('my_date','') # for eg. 2015-10-26
my_date = datetime.strptime(my_date, "%Y-%m-%d")
events = FacebookEvent.objects.filter(start_time__lt=my_date, end_time__gt=my_date).order_by('start_time')
#4
0
Slightly late to the party here, but if you're planning to use this range
feature frequently, I highly recommend checking out the new postgres range fields. They have a lot of handy query filters right off the bat and provide a much cleaner, intuitive way to deal with ranges.
这里的聚会稍微晚了一点,但是如果您打算经常使用这个范围特性,我强烈建议您查看新的postgres范围字段。它们有很多方便的查询过滤器,并且提供了一种更干净、更直观的方式来处理范围。
#1
1
Im not sure if I understand the question.
我不确定我是否理解这个问题。
If you have a model instance and a date:
如果您有一个模型实例和一个日期:
First you need to convert your date to a datetime.datetime object
首先需要将日期转换为datetime。datetime对象
from dateutil.parser import parse
my_date= request.POST.get('my_date','')
mdate = parse(mydate)
Then you can use mdate to do logical comparisons. i.e: mdate > fbEvent.start_time
然后可以使用mdate进行逻辑比较。我。艾凡:mdate > fbEvent.start_time
If you want model instances between dates
如果需要在日期之间建立模型实例
mdate = parse(mydate)
events = FacebookEvent.objects.filter(start_time__gt=mdate, end_time__lt=mdate)
#2
0
The following should help you get started:
以下内容可以帮助你开始:
# assuming Sample is your model class and sampledate is your column to filter on
samples = Sample.objects.filter(sampledate__gt=datetime.date(2011, 1, 1), sampledate__lt=datetime.date(2011, 1, 31))
#3
0
you can do like:
你能做的:
from datetime import datetime
my_date= request.POST.get('my_date','') # for eg. 2015-10-26
my_date = datetime.strptime(my_date, "%Y-%m-%d")
events = FacebookEvent.objects.filter(start_time__lt=my_date, end_time__gt=my_date).order_by('start_time')
#4
0
Slightly late to the party here, but if you're planning to use this range
feature frequently, I highly recommend checking out the new postgres range fields. They have a lot of handy query filters right off the bat and provide a much cleaner, intuitive way to deal with ranges.
这里的聚会稍微晚了一点,但是如果您打算经常使用这个范围特性,我强烈建议您查看新的postgres范围字段。它们有很多方便的查询过滤器,并且提供了一种更干净、更直观的方式来处理范围。