On Fri, Feb 17, 2012 at 12:13 PM, Tom Evans <[email protected]> wrote:
> On Fri, Feb 17, 2012 at 12:00 PM, andreas <[email protected]> wrote:
>> Sorry for the vague description, but i find it hard to exactly name
>> the problem i am facing. Hopefully my description is clear enough:
>>
>> I am working on a reporting app that collects and displays events/
>> actions that happened in my application.
>>
>> I wrote a litte plugin base class and added plugins for all of the
>> relevant events i am interested in.
>> Basically a plugin only defines the query that is used to return the
>> matching events.
>>
>> Now i am facing the situation that the queries are only evaluated
>> once:
>> A view that instantiates all my plugins and collects their events
>> (get_plugin_data) always returns the same data unless i restart my
>> server.
>>
>>        class DigestPluginBase(object):
>>
>>            def get_plugin_data(self):
>>                if self.queryset:
>>                    data = list(self.queryset)
>>                else:
>>                    data = []
>>                return data
>>
>         class UpdatesNew(DigestPluginBase):
>               def __init__(self, *args, **kwargs):
>                   self.queryset =
> Update.objects.filter(timestamp__gte=yesterday)
>>
>> Querysets are lazy, so i assume this is a problem with the way i
>> assign/define the queryset for my plugin.
>> But then i don't see how the old data is stored in my class as the
>> there should be a new instance (and new queries) of my class every
>> time the view is called.
>> So where do i go wrong?
>>
>> On a side note: Can you think of more elegant ways to define the
>> querysets for my plugins?
>
> FTFY
>
> How you had it before set a class level attribute. It exists on each
> instance, but only as a copy of that on the class. Set it up as an
> instance attribute (see inline fix) and it will be recalculated each
> time your class is instantiated.
>
> Cheers
>
> Tom

Oh, in my rush I failed to spot the variable 'yesterday'. That will
only be yesterday when that datetime object was instantiated (not
shown in your snippet). You would need to re-instantiate that each
time you want to evaluate the queryset.

Cheers

Tom

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