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. While the database stores everything as utc as it should, the entire application is based solely on Eastern Time. All I want to do is filter objects that happened >= to 12:00 AM EST/EDT
RuntimeWarning: DateTimeField received a naive datetime (2013-08-13 00:00:00) while time zone support is active.
The functionality I am after is to check how many entries a user has for today in Eastern time.
Using 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()
Did you import from your models Entry?
from models import Entry
If you want to get all the object created after 12AM. ( I suppose all elements created or modified today?)
my 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.
in 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 }
)
the "mydate" is all the field that Mecdatebis have (text1 and date1) the now is equal to the date on your system
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.