I also though about mutable variable as default paramter but I doubt it is.
Indeed, I should have pasted the code from the start, so here we go.
Note that those are parts of the django-cms blog plugin called cmsplugin-blog (http://www.django-cms.org/en/extensions/?page=3)
In urls.py, I have:
blog_info_dict = {
'queryset': Entry.published.all(),
'date_field': 'pub_date',
}
and in the url pattern:
(r'^$', 'django.views.generic.date_based.archive_index', blog_info_dict, 'blog_archive_index'),
Then, in the published manager:
def get_query_set(self):
now = datetime.datetime.now()
print now
return super(PublishedEntriesManager, self).get_query_set() \
.filter(is_published=True, pub_date__lte=now)
My issue is, with the dev server or with gunicorn, I only have the "print now" for the first request. Whenever I reload the page, I can't get it again, I have to restart the server.
I know django-cms uses cache. However, debug-toolbar tells me that the request is being processed so I assume it is not cached.
Would it be possible that it only caches one part of the queryset ?
Regards,
Xavier.
Le 24 juin 2010 à 21:39, Daniel Roseman a écrit :
> On Jun 24, 6:36 pm, Xavier Ordoquy <xordo...@linovia.com> wrote:
>> Hi,
>>
>> I'm banging my head against the wall with an issue in get_query_set.
>>
>> I'm using django 1.2.1 and I'd like to filter the queryset results through a date within get_query_set.
>> However, I noticed that with the dev. server or gunicorn, the get_query_set is only called on the first request.
>>
>> This manager is called from a queryset passed to a generic function so unless those functions have builtin cache, I don't understand why the get_query_set is only called on the first request.
>> Has anyone an idea why this happens ?
>
> From your description, it sounds like you're doing something like
> this:
>
> def my_function(queryset=MyModel.objects.all())
> return queryset.filter(whatever=whatever)
>
> This is a standard Python gotcha, nothing to do with Django: never use
> a mutable variable as a default parameter to a function. It is
> executed *when the function is first defined*, not when it's called.
> Use 'queryset=None' instead and set the default within the function.
>
> If this isn't what you're doing, you need to show some actual code.
> --
> DR.
>
> --
> 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 django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment