Re: confusing query involving time ranges

2010-08-13 Thread Emily Rodgers
On Aug 13, 11:18 am, Steven Davidson wrote: > > It would have to be another datetime field rather than an is_current flag > > because an is_current flag wouldn't help me for queries in the past (if that > > makes sense). > > Ah yes, of course, I had not fully grasped that bit. A nullable foreign

Re: confusing query involving time ranges

2010-08-13 Thread Steven Davidson
> > It would have to be another datetime field rather than an is_current flag > because an is_current flag wouldn't help me for queries in the past (if that > makes sense). Ah yes, of course, I had not fully grasped that bit. A nullable foreign key reference to the State that supersedes it might

Re: confusing query involving time ranges

2010-08-13 Thread Emily Rodgers
On Aug 13, 9:51 am, Steven Davidson wrote: > Hmm, I can't fathom it. > > I would opt for a single simple query that returns a little more than you > need and post-process it in python. This would be more maintainable than a > hairy ORM query, I'd say; but if you have vast numbers of results that

Re: confusing query involving time ranges

2010-08-13 Thread Steven Davidson
Hmm, I can't fathom it. I would opt for a single simple query that returns a little more than you need and post-process it in python. This would be more maintainable than a hairy ORM query, I'd say; but if you have vast numbers of results that may not be appropriate. Alternatively, and presuming

Re: confusing query involving time ranges

2010-08-13 Thread Emily Rodgers
On Aug 12, 8:26 pm, Alec Shaner wrote: > Hopefully some django sql guru will give you a better answer, but I'll take > a stab at it. > > What you describe does sound pretty tricky. Is this something that has to be > done in a single query statement? It doesn't have to be, but I would like to try

Re: confusing query involving time ranges

2010-08-13 Thread Paulo Almeida
I see. What about Alec Shaner's suggestion? If you replace 'order_by' with 'latest' it will be similar to my suggestion with just two queries. - Paulo On Fri, Aug 13, 2010 at 8:28 AM, Emily Rodgers wrote: > On Aug 12, 10:00 pm, Paulo Almeida > wrote: > > Can you sequentially add the states for

Re: confusing query involving time ranges

2010-08-13 Thread Emily Rodgers
On Aug 12, 10:00 pm, Paulo Almeida wrote: > Can you sequentially add the states for each foo? I'm thinking of something > like: > > states = [] > for foo in foos: >     foo_state = State.objects.filter(foo=foo, first_dt__lte=dt, > last_dt__gte=dt) >     if foo_state: >         states.append(foo_st

Re: confusing query involving time ranges

2010-08-12 Thread Paulo Almeida
Can you sequentially add the states for each foo? I'm thinking of something like: states = [] for foo in foos: foo_state = State.objects.filter(foo=foo, first_dt__lte=dt, last_dt__gte=dt) if foo_state: states.append(foo_state) else: states = State.objects.filter(foo=foo

Re: confusing query involving time ranges

2010-08-12 Thread Alec Shaner
Hopefully some django sql guru will give you a better answer, but I'll take a stab at it. What you describe does sound pretty tricky. Is this something that has to be done in a single query statement? If you just need to build a list of objects you could do it in steps, e.g.: # Get all State obje

confusing query involving time ranges

2010-08-12 Thread Emily Rodgers
Hi, I am a bit stuck on this and can't seem to figure out what to do. I have a model that (stripped down for this question) looks a bit like this: class State(models.Model): first_dt = models.DateTimeField(null=True) last_dt = models.DateTimeField(null=True) foo = models.CharField(Fo