简体   繁体   English

Django查询按日期过滤

[英]Django Query Filter by Date

I am trying to query/filter a table for events that happened today eastern time, but I keep running into issues. 我正在尝试查询/过滤表以查找今天东部时间发生的事件,但是我一直遇到问题。 It returns my expected result but I get a runtime warning. 它返回我的预期结果,但我收到运行时警告。 Iv't tried about 15 different implementations, but I either get a runtime warning or it doesn't return the expected data. 我没有尝试过15种不同的实现,但是我要么收到了运行时警告,要么没有返回预期的数据。 While the database stores everything as utc as it should, the entire application is based solely on Eastern Time. 尽管数据库将所有内容都保存为utc,但整个应用程序仅基于美国东部时间。 All I want to do is filter objects that happened >= to 12:00 AM EST/EDT 我要做的就是过滤发生的事件,这些事件> =到EST / EDT AM 12:00

RuntimeWarning: DateTimeField received a naive datetime (2013-08-13 00:00:00) while time zone support is active. RuntimeWarning:时区支持处于活动状态时,DateTimeField收到的日期时间很短(2013-08-13 00:00:00)。

The functionality I am after is to check how many entries a user has for today in Eastern time. 我要执行的功能是检查东部时间今天用户有多少条目。

Using Sqlite3 使用Sqlite3

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

Here are some of the ways i tired to get todays date: 这是我厌倦了今天约会的一些方法:

    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))

Does any one have an easy way to get objects with a time stamp >= to today? 是否有人有一种简单的方法来获取时间戳大于等于今天的对象?

Just come up with something else: 只是想出别的东西:

by doing a queryset: 通过执行一个查询集:

today= todaydate (use timezone to get it)
today_entry = Entry.objects.filter(date1=today).count()

can you try? 你能试一下吗?

count_reward = Rewards.objects.filter(user=request.user).filter(routine=d).count() count_reward = Rewards.objects.filter(user = request.user).filter(routine = d).count()

Did you import from your models Entry? 您是否从模型入口中导入?

from models import Entry

If you want to get all the object created after 12AM. 如果要获取在12AM之后创建的所有对象。 ( I suppose all elements created or modified today?) (我想今天是否创建或修改了所有元素?)

my models.py 我的models.py

class Mecdatebis(models.Model):
    text1 = models.CharField(max_length=50)
    date1 = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.text1

i create a model that have a CharField (to recognize if the entry appears in my html for the good one) a DateTimeField(auto_now=True) that create the Timestamp you are looking for. 我创建了一个具有CharField(可识别条目是否出现在我的html中的好条目)和DateTimeField(auto_now = True)的模型,该模型创建了您要查找的时间戳。

in my views.py: 在我的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 }
        )

the "mydate" is all the field that Mecdatebis have (text1 and date1) the now is equal to the date on your system “ mydate”是Mecdatebis拥有的所有字段(text1和date1),现在等于系统上的日期

then on my template: 然后在我的模板上:

{% 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 %}

My test is as follow: If the time that I have now on my system is == at the year of my timestamp and if the month of my date now is == of the month of my timestamp and if the day (the number) of my date now is == of the timestamp day. 我的测试如下:如果我现在在系统上的时间是==时间戳的年份,并且现在日期的月份是==时间戳的月份,并且日期(数字)我现在的日期是时间戳记日期的==。 Then show me all the entry of the same date. 然后显示所有相同日期的条目。

So at least we get the object. 因此,至少我们得到了对象。 but counting them I had difficulty. 但算上他们我有困难。 I tried to apply the same method in the views. 我试图在视图中应用相同的方法。 without success I hope someone can come here and get something working. 没有成功,我希望有人可以来这里工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM