[Django]-Django's list_details views saving queryset to memory (not updating)?

4👍

It is not a problem of cache: as you do it now, the queryset definition is evaluated once, while parsing urls, and then, it is never evaluated again.

Solution is actually pretty simple and described in the online documentation: Complex filtering with wrapper functions: just create a small custom view, that will simply call the generic view.
I am actually using a similar solution quite a lot and I feel it quite comfortable.

By the way, a small side note, for this case I would suggest not using a custom manager, and go back instead on a normal filtering.

👤rob

2👍

Try correcting urls.py to:

url(r'^video/(?P<object_id>\d+)$', 
    list_detail.object_detail,
    {   'queryset': Video.objects.all, }, # here's the difference
    name='video_detail',
)

Edit:

If this fail, try apply similar technique(passing callable instead of calling it) to filter():

return queryset.filter(pub_date__lte=datetime.utcnow)
👤gorsky

1👍

I have an almost identical model Manager to thornomad, and the same problem with generic views.

I have to point out that neither of the suggestions above work:

  • doing Video.objects.all without parentheses gives an error
  • doing queryset.filter(pub_date__lte=datetime.utcnow), again without the parentheses, does not give an error but does not fix the problem

I have also tried another way, which is to use a lambda to return the queryset, eg:

qs = lambda *x: Video.objects.all()
url(r'^video/(?P<object_id>\d+)$', 
    list_detail.object_detail,
    {   'queryset': qs(), },
    name='video_detail',
),

…it didn’t work either and I can see now I must have been desperate to think it would 🙂

lazy_qs = lambda *x: lazy(Post.live_objects.all, QuerySet)

blog_posts = {
    'queryset': lazy_qs(),

…doesn’t work either (gives an error) as utils.functional.lazy doesn’t know how to convert the result to a QuerySet properly, as best I can tell.

I think Roberto’s answer of wrapping the generic view is the only one that will help.

The django docs should be amended to point out the limitations of the queryset used by generic views (currently the docs have a special note to tell you everything will be okay!)

Leave a comment