Re: Need help filtering a queryset

2008-07-01 Thread Brandon Taylor

Thanks very much for the advice Brian. I'll definitely switch that up
and try both approaches. I hadn't seen another way to get at a subset
of objects other than:

model_set.all

but now I do, thanks to your example.

Kind regards,
Brandon

On Jul 1, 12:28 pm, Brian Luft <[EMAIL PROTECTED]> wrote:
> Another approach would be to add a method to your events model. Name
> it something like "current_occurrences" and have it return the
> filtered occurrences. Ex:
>
> class event( models.Model ):
>    ...
>    def current_occurrences( self ):
>        return
> self.occurrence_set.filter( date__gte=datetime.datetime.today() )
>
> Then you would change your template to
>
> {% for occurrence in event.current_occurences %}
>
> That should offer a little better performance since you wouldn't be
> pulling the entire occurrence set for each event.
>
> If performance is a major factor, investigate writing a custom model
> manager for your events model.  That might be the best option for
> retrieving the joined set of events with current occurrences in one
> fell swoop in a nice abstracted manner.
>
> -Brian
>
> On Jul 1, 10:12 am, Brian Luft <[EMAIL PROTECTED]> wrote:
>
> > Ok, you are asking for a list of events that have occurences on or
> > after today's date.  Then with each event instance you are asking for
> > ALL occurence instances (event.occurrence_set.all). ALL means EVERY
> > occurence associated with that event object.  You haven't done
> > anything to filter out occurrences based on date in that step.
>
> > -Brian
>
> > On Jul 1, 9:51 am, Brandon Taylor <[EMAIL PROTECTED]> wrote:
>
> > > Hi everyone,
>
> > > I have Events and Occurrences of an Event. Occurrence contains a
> > > foreign_key for the Event.
> > > In my template, I need to display the Event and Occurrences as such:
>
> > > Event Title One
> > >     June 1, 2008 [times]
> > >     July 23, 2008 [times]
>
> > > Event Title Two
> > >     August 25, 2008 [times]
> > >     September 1, 2008 [times]
>
> > > But, I only want to display dates that are greater than, or equal to
> > > today. In my view, I am selecting Events as such:
>
> > > events_list =
> > > events.filter(occurrence__date__gte=datetime.datetime.today())
>
> > > Here's my template code:
> > > 
> > >     {% for event in events_list %}
> > >         {{ event.title }}
> > >               {% for occurrence in event.occurrence_set.all %}
> > >                   
> > >                       
> > >                           {{ occurrence.date }}
> > >                       
> > >                   
> > >               {% endfor %}
> > >         
> > >     {% endfor %}
> > > 
>
> > > Which is showing ALL of the occurrences, not just the ones with a date
> > > greater than today. Given the sample data set above, it shouldn't
> > > display the event for June. Can someone please help me out with this?
> > > I'd really appreciate some advice.
>
> > > TIA,
> > > Brandon
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Need help filtering a queryset

2008-07-01 Thread Brian Luft

Another approach would be to add a method to your events model. Name
it something like "current_occurrences" and have it return the
filtered occurrences. Ex:

class event( models.Model ):
   ...
   def current_occurrences( self ):
   return
self.occurrence_set.filter( date__gte=datetime.datetime.today() )

Then you would change your template to

{% for occurrence in event.current_occurences %}

That should offer a little better performance since you wouldn't be
pulling the entire occurrence set for each event.

If performance is a major factor, investigate writing a custom model
manager for your events model.  That might be the best option for
retrieving the joined set of events with current occurrences in one
fell swoop in a nice abstracted manner.

-Brian

On Jul 1, 10:12 am, Brian Luft <[EMAIL PROTECTED]> wrote:
> Ok, you are asking for a list of events that have occurences on or
> after today's date.  Then with each event instance you are asking for
> ALL occurence instances (event.occurrence_set.all). ALL means EVERY
> occurence associated with that event object.  You haven't done
> anything to filter out occurrences based on date in that step.
>
> -Brian
>
> On Jul 1, 9:51 am, Brandon Taylor <[EMAIL PROTECTED]> wrote:
>
> > Hi everyone,
>
> > I have Events and Occurrences of an Event. Occurrence contains a
> > foreign_key for the Event.
> > In my template, I need to display the Event and Occurrences as such:
>
> > Event Title One
> >     June 1, 2008 [times]
> >     July 23, 2008 [times]
>
> > Event Title Two
> >     August 25, 2008 [times]
> >     September 1, 2008 [times]
>
> > But, I only want to display dates that are greater than, or equal to
> > today. In my view, I am selecting Events as such:
>
> > events_list =
> > events.filter(occurrence__date__gte=datetime.datetime.today())
>
> > Here's my template code:
> > 
> >     {% for event in events_list %}
> >         {{ event.title }}
> >               {% for occurrence in event.occurrence_set.all %}
> >                   
> >                       
> >                           {{ occurrence.date }}
> >                       
> >                   
> >               {% endfor %}
> >         
> >     {% endfor %}
> > 
>
> > Which is showing ALL of the occurrences, not just the ones with a date
> > greater than today. Given the sample data set above, it shouldn't
> > display the event for June. Can someone please help me out with this?
> > I'd really appreciate some advice.
>
> > TIA,
> > Brandon
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Need help filtering a queryset

2008-07-01 Thread Brian Luft

Ok, you are asking for a list of events that have occurences on or
after today's date.  Then with each event instance you are asking for
ALL occurence instances (event.occurrence_set.all). ALL means EVERY
occurence associated with that event object.  You haven't done
anything to filter out occurrences based on date in that step.

-Brian

On Jul 1, 9:51 am, Brandon Taylor <[EMAIL PROTECTED]> wrote:
> Hi everyone,
>
> I have Events and Occurrences of an Event. Occurrence contains a
> foreign_key for the Event.
> In my template, I need to display the Event and Occurrences as such:
>
> Event Title One
>     June 1, 2008 [times]
>     July 23, 2008 [times]
>
> Event Title Two
>     August 25, 2008 [times]
>     September 1, 2008 [times]
>
> But, I only want to display dates that are greater than, or equal to
> today. In my view, I am selecting Events as such:
>
> events_list =
> events.filter(occurrence__date__gte=datetime.datetime.today())
>
> Here's my template code:
> 
>     {% for event in events_list %}
>         {{ event.title }}
>               {% for occurrence in event.occurrence_set.all %}
>                   
>                       
>                           {{ occurrence.date }}
>                       
>                   
>               {% endfor %}
>         
>     {% endfor %}
> 
>
> Which is showing ALL of the occurrences, not just the ones with a date
> greater than today. Given the sample data set above, it shouldn't
> display the event for June. Can someone please help me out with this?
> I'd really appreciate some advice.
>
> TIA,
> Brandon
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Need help filtering a queryset

2008-07-01 Thread Brandon Taylor

I made a custom filter that tests for the date:

import datetime

from django import template
register = template.Library()

@register.filter('is_current')
def isPast(occurrence):
if occurrence.date >= datetime.datetime.today().date():
return True
return False


which works, but is this the best way this can be handled? I'm still
relatively new to Django, and I can't help but think this can be
handled at the Database API level in some way that I'm overlooking.

Thoughts? Advice greatly appreciated.

On Jul 1, 11:51 am, Brandon Taylor <[EMAIL PROTECTED]> wrote:
> Hi everyone,
>
> I have Events and Occurrences of an Event. Occurrence contains a
> foreign_key for the Event.
> In my template, I need to display the Event and Occurrences as such:
>
> Event Title One
>     June 1, 2008 [times]
>     July 23, 2008 [times]
>
> Event Title Two
>     August 25, 2008 [times]
>     September 1, 2008 [times]
>
> But, I only want to display dates that are greater than, or equal to
> today. In my view, I am selecting Events as such:
>
> events_list =
> events.filter(occurrence__date__gte=datetime.datetime.today())
>
> Here's my template code:
> 
>     {% for event in events_list %}
>         {{ event.title }}
>               {% for occurrence in event.occurrence_set.all %}
>                   
>                       
>                           {{ occurrence.date }}
>                       
>                   
>               {% endfor %}
>         
>     {% endfor %}
> 
>
> Which is showing ALL of the occurrences, not just the ones with a date
> greater than today. Given the sample data set above, it shouldn't
> display the event for June. Can someone please help me out with this?
> I'd really appreciate some advice.
>
> TIA,
> Brandon
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Need help filtering a queryset

2008-07-01 Thread Brandon Taylor

Hi everyone,

I have Events and Occurrences of an Event. Occurrence contains a
foreign_key for the Event.
In my template, I need to display the Event and Occurrences as such:

Event Title One
June 1, 2008 [times]
July 23, 2008 [times]

Event Title Two
August 25, 2008 [times]
September 1, 2008 [times]


But, I only want to display dates that are greater than, or equal to
today. In my view, I am selecting Events as such:

events_list =
events.filter(occurrence__date__gte=datetime.datetime.today())


Here's my template code:

{% for event in events_list %}
{{ event.title }}
  {% for occurrence in event.occurrence_set.all %}
  
  
  {{ occurrence.date }}
  
  
  {% endfor %}

{% endfor %}


Which is showing ALL of the occurrences, not just the ones with a date
greater than today. Given the sample data set above, it shouldn't
display the event for June. Can someone please help me out with this?
I'd really appreciate some advice.

TIA,
Brandon
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---