Python Django查询按日期筛选
我试图查询/筛选一个表,查找东部时间今天发生的事件,但我一直遇到问题。它返回我的预期结果,但我得到一个运行时警告。我没有尝试过15种不同的实现,但我要么收到运行时警告,要么没有返回预期的数据。虽然数据库按utc格式存储所有内容,但整个应用程序完全基于东部时间。我所要做的就是过滤发生在美国东部时间上午12:00之前>=的对象 RuntimeWarning:DateTimeField在时区支持处于活动状态时收到原始日期时间(2013-08-13 00:00:00) 我想要的功能是检查一个用户在东部时间今天有多少条记录 使用Sqlite3Python Django查询按日期筛选,python,django,filter,warnings,Python,Django,Filter,Warnings,我试图查询/筛选一个表,查找东部时间今天发生的事件,但我一直遇到问题。它返回我的预期结果,但我得到一个运行时警告。我没有尝试过15种不同的实现,但我要么收到运行时警告,要么没有返回预期的数据。虽然数据库按utc格式存储所有内容,但整个应用程序完全基于东部时间。我所要做的就是过滤发生在美国东部时间上午12:00之前>=的对象 RuntimeWarning:DateTimeField在时区支持处于活动状态时收到原始日期时间(2013-08-13 00:00:00) 我想要的功能是检查一个用户在东部时
Settings.py:
USE_TZ = True
TIME_ZONE = 'America/New_York'
Models.py
class Entry(models.Model):
id = models.AutoField(primary_key=True, null=False)
prize_id = models.ForeignKey(Prize)
member = models.ForeignKey(Member)
createdate = models.DateTimeField(auto_now_add=True)
Views.py
from django.utils import timezone
from datetime
#this is part of my view class
def get_user_entries(memberid, prizeid):
today = datetime.date.today()
e = Entry.objects.filter(member_id=memberid
).filter(prize_id=prizeid
).filter(createdate__gte=today
).count()
return e
以下是我今天约会的一些方式:
today = datetime.date.today()
today = '%s-%s-%s' % (today.year, today.month, today.day)
date = datetime.date.today()
time = datetime.time(00, 00, 00, 000000)
combo = datetime.datetime.combine(date, time)
hours = timezone.now().hour
minutes = timezone.now().minute
seconds = timezone.now().second
mseconds = timezone.now().microsecond
#convert timezone now to utc 04:00:00:000000 UTC
# this works now and doesnt throw a runtime error and gives me the correct data im expecting, but i think it will be an hour off during dst
t = timezone.now().replace(hour=04, minute=00, second=00, microsecond=000000)
#I also tired this but still didnt get the expected results:
e = Entry.objects.filter(member_id=1
).filter(prize_id=1
).filter(createdate__gte=timezone.now() -
datetime.timedelta(hours=datetime.datetime.now().hour,
minutes=datetime.datetime.now().minute,
seconds=datetime.datetime.now().second,
microseconds=datetime.datetime.now().microsecond))
有没有人能找到一种简单的方法来获取带有时间戳>=today的对象?只要想出其他方法: 通过执行查询集:
today= todaydate (use timezone to get it)
today_entry = Entry.objects.filter(date1=today).count()
你能试试吗
count\u raward=rawards.objects.filter(用户=request.user).filter(例程=d).count()
是否从模型条目中导入
from models import Entry
如果您希望在上午12点之后创建所有对象。(我想今天创建或修改的所有元素?)
我的模特
class Mecdatebis(models.Model):
text1 = models.CharField(max_length=50)
date1 = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.text1
我创建了一个具有CharField的模型(以识别该条目是否出现在我的html中)
DateTimeField(auto_now=True),用于创建要查找的时间戳
在my views.py中:
from models import Mecdatebis
from datetime import *
def date(request):
now = datetime.now()
test = Mecdatebis.objects.all() #get all the data from your models
return render_to_response('date.html', {
'mydate': test,
'now' : now }
)
“mydate”是Mecdatebis拥有的所有字段(text1和date1)
现在等于系统上的日期
然后在我的模板上:
{% for mesdate in mydate %}
{% if now.year == mesdate.date1.year and now.month == mesdate.date1.month and now.day == mesdate.date1.day %}
{{mesdate.text1}}
{% endif %}
{% endfor %}
我的测试如下:
如果我现在在我的系统上的时间是==在我的时间戳的年份
如果我现在的日期月份是==我的时间戳月份
如果我现在的日期(数字)是时间戳日期的==天。然后显示同一日期的所有条目
所以至少我们得到了目标。
但是数一数我有困难。
我尝试在视图中应用相同的方法。无功而返
我希望有人能来这里工作。尝试使用
django.utils.timezone
模块来创建日期时间,而不是datetime
模块。