Tim Chase wrote:
>> status.effectivedate is when a status 'starts', (like closed for repairs.)
>> How do I get the 'current status based on current date' ?
>>
>> so if the following 3 records are in the table:
>> status
>> effective date, description
>> 1/1/2007 - open for business
>> 3/24/2007 - closed for remodeling
>> 5/1/2007 - open with a new look.
>> 11/14/2007 - closed for Christmas
>> 1/1/2008 - open - happy new year.
>>
>> So right now, the status is "open with a new look."   when it is 11/14, the 
>> current status will be "closed for Christmas."
>>
>> The SQL is a big ugly, hoping the ORM will make it for me.
> 
> IIUC, you want the most recent status with a date earler than 
> now, which should be doable via something like
> 
>   from datetime import datetime
>   status = Status.objects.filter(
>     effectivedate__lt = datetime.now()
>     ).latest('effectivedate')
> 

nice.

> 
> I'm not sure the SQL is all that ugly either, as it would be 
> something like
> 
>    SELECT *
>    FROM app_status
>    WHERE effectivedate < NOW()
>    ORDER BY effectivedate DESC
>    LIMIT 1

Also nice. never seen it done with LIMIT.       

    SELECT *
    FROM app_status
    WHERE effectivedate in
     (select max(effectivedate)
       from app_status
        where effectivedate<=NOW() )

and good luck augmenting that with WHERE someID=foo, cuz you have to add that 
into both WHERE's.

> 
> but it's nice to have it abstracted in the ORM to avoid backend 
> peculiarities regarding the NOW function and the syntax for 
> limiting to one row.  ("now()" vs. "curdate"; "LIMIT" vs. "TOP")
> 

once I get something working (probably with the filter you suggested) I'll post 
a trac about a more ... elegant? version.

> As a side note, if you work someplace that closes for Christmas 
> on 11/14, I want to work there! :)

yeah, cuz that place is gonna be in business for a while.

Carl K

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