> What I want to do is get post between 9:00AM-5:00PM on any day.  I
> just want to compare Hour part of "created_at" field.  If the hour is
> between 9-5, can be any day, get the matching records.
> 
> Pseudo Django Python Code:
> 
> Post.objects.get( Q(created_at = (9:00AM - 5:00PM)))
> 
> Is this possible?

Yes, it's possible.

First off, you'd /generally/ want to likely use .filter() instead
of .get() as .get() is designed for getting a single existing
row.  The .filter() function returns zero-or-more rows which is
likely what you want.

However in *this* case, because Django doesn't have an __hour
pseudo-property for the filter clause like it has a
pseudo-year/month/day property as described at

http://www.djangoproject.com/documentation/db-api/#year

there's no abstracted way to do it that will work across all the
DB backends.

You can, however, use the .extra() call to pass an additional
WHERE clause to your query object.  This would take the form
something like

  Post.objects.extra(where="""
    hour(app_post.created_at) BETWEEN 9 AND 5+12
    """)

However, the syntax varies based on your SQL backend.  The above
should work for MySQL.  For sqlite, it would be something like

   strftime('%y', app_post.created_at) BETWEEN 9 AND 5+12

whereas for PostgreSQL, you would want

   Extract(hour FROM app_post.created_at) BETWEEN 9 AND 5+12

I'm not 100% positive on the Sqlite variant, as I'm not sure how
smart it is about dealing with strings vs. integers (it's a
little overhelpful in converting, so you might need a
cast()/convert() sort of call to push it to an integer before
comparing)

-tim




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

Reply via email to