Re: Querying for objects created on a certain date

2009-10-12 Thread aa56280

Karen,

I'm aware that my 30-days-ago version of the query is current time/
date specific. Good catch :-)

Thanks for the solution. I also found this one:
today = datetime.datetime.today()
Document.objects.filter(created_on__month=today.month,
created_on__day=today.day, created_on__year=today.year)

Thanks again.


On Oct 12, 5:00 pm, Karen Tracey  wrote:
> On Mon, Oct 12, 2009 at 5:07 PM, aa56280  wrote:
>
> > I have a DateTimeField called "created_on" on a model called
> > "Documents". It serves as a timestamp for when the record was added.
>
> > I'm trying to write a query that will return all Documents created_on
> > a particular date, say today. I can make it work if I look for a
> > record within a range of dates...
>
> > 
> > today = datetime.datetime.today()
> > thirty_days_ago = today - datetime.timedelta(days=30)
> > Document.objects.filter(created_on__range=(thirty_days_ago, today))
> > 
>
> > But now, I need to find records that were created on a particular
> > date, like, today.
>
> > How do I do that?
>
> today_end = datetime.datetime.combine(datetime.date.today(),
> datetime.time.max)
> today_start = datetime.datetime.combine(datetime.date.today(),
> datetime.time.min)
> Document.objects.filter(created_on__range=(today_start, today_end))
>
> You realize your 30-days-ago version of the query returns values that depend
> on the current time as well as the current date when the query is run? This
> may be rather surprising behavior for people who would likely expect the
> list of things created within the last 30 days to be constant over the
> course of a day (excepting things added during the day).
>
> Karen
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Querying for objects created on a certain date

2009-10-12 Thread Karen Tracey
On Mon, Oct 12, 2009 at 6:00 PM, phoebebright wrote:

>
> Try this:
>
> from datetime import date
>
> today = date.today()
>
> todays_stuff = Stuff.objects.filter(created_on = today)
>
>
Looking at the SQL that generates for a model that contains a DateTimeField,
not a DateField, I don't believe that will do what's wanted:

>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2009, 10, 12)
>>> Document.objects.filter(created_on=today)
[]
>>> from django.db import connection
>>> connection.queries[-1]['sql']
u'SELECT `ttt_document`.`id`, `ttt_document`.`created_on`,
`ttt_document`.`name` FROM `ttt_document` WHERE `ttt_document`.`created_on`
= 2009-10-12 00:00:00  LIMIT 21'
>>>

It will only find stuff created at midnight, exactly.  (Unless the same
use-a-date-for-a-datetime technique is used when adding as well, so that the
times will always be 0, in which case it ought to be just a DateField.)

Karen

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Querying for objects created on a certain date

2009-10-12 Thread phoebebright

Try this:

from datetime import date

today = date.today()

todays_stuff = Stuff.objects.filter(created_on = today)



On Oct 12, 10:07 pm, aa56280  wrote:
> I have a DateTimeField called "created_on" on a model called
> "Documents". It serves as a timestamp for when the record was added.
>
> I'm trying to write a query that will return all Documents created_on
> a particular date, say today. I can make it work if I look for a
> record within a range of dates...
>
> 
> today = datetime.datetime.today()
> thirty_days_ago = today - datetime.timedelta(days=30)
> Document.objects.filter(created_on__range=(thirty_days_ago, today))
> 
>
> But now, I need to find records that were created on a particular
> date, like, today.
>
> How do I do that?
>
> Any thoughts greatly appreciated.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Querying for objects created on a certain date

2009-10-12 Thread Karen Tracey
On Mon, Oct 12, 2009 at 5:07 PM, aa56280  wrote:

>
> I have a DateTimeField called "created_on" on a model called
> "Documents". It serves as a timestamp for when the record was added.
>
> I'm trying to write a query that will return all Documents created_on
> a particular date, say today. I can make it work if I look for a
> record within a range of dates...
>
> 
> today = datetime.datetime.today()
> thirty_days_ago = today - datetime.timedelta(days=30)
> Document.objects.filter(created_on__range=(thirty_days_ago, today))
> 
>
> But now, I need to find records that were created on a particular
> date, like, today.
>
> How do I do that?
>
>
today_end = datetime.datetime.combine(datetime.date.today(),
datetime.time.max)
today_start = datetime.datetime.combine(datetime.date.today(),
datetime.time.min)
Document.objects.filter(created_on__range=(today_start, today_end))

You realize your 30-days-ago version of the query returns values that depend
on the current time as well as the current date when the query is run? This
may be rather surprising behavior for people who would likely expect the
list of things created within the last 30 days to be constant over the
course of a day (excepting things added during the day).

Karen

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---