On Mon, 2007-04-30 at 18:02 +0200, Nicolas Steinmetz wrote:
> gsmith wrote:
> 
> > 
> > Hello,
> > I have a template file that when shown is going to be populated by
> > data from two different tables.  I want to use the generic view
> > 'list_detail.object_list'.  However, as far as I know I can only pass
> > one queryset to the object_list view.  Is there a way that I can pass
> > two QuerySet's?
> > 
> > For example this is how it works now
> > 
> > rso_news_info = {
> >     "queryset" : news.objects.all(),
> >     "template_object_name" : "news",
> > }
> > 
> > I am wondering if I can use something like
> > 
> > rso_news_info = {
> >     "queryset" : news.objects.all(),
> >     "queryset" : leftnav.objects.all()
> >     "template_object_name" : "news",
> > }
> > 
> > So that I would be able to have two block tags in my template
> > news_list.html that display the data from both tables.
> > 
> > Any help would be appreciated.
> 
> I suggest you using extra_context parameter to add your objects within.
> 
> Ex :
> 
> urlpatterns = patterns('',
>    (r'^$', 'django.views.generic.list_detail.object_list', dict(queryset=
> Post.objects.all(),extra_context={'all_tags':
> Tag.objects.all(), 'all_categories': Category.objects.all(),} )),


One important thing to be aware of when using this method: generic views
are very careful to always re-evaluate the queryset parameter you pass
in so that it always contains up-to-date information. Remember that
querysets are cached, so that the first time the retrieve the data from
the database and the second and subsequent times, it is in memory (and
hence faster).

What we don't do (because it's impossible to do, often) is re-evaluate
the arguments passed in extra_context. So, if you are passing a queryset
in there, it will be evaluated the first time through the view and the
results pulled from database. On subsequent calls, the *same* results
will be used, because the dictionary passed in was constructed at the
time urls.py was imported and subsequently re-used. To see the problem,
call your view, then, without stopping the webserver, add a new category
object to the database, call the view again and notice that the new
category isn't reflected in the result -- because the database isn't
being re-queried.

If you want querysets in extra_context to be truly fresh, wrap them in a
lambda or a function and pass that in. Django will evaluate any
callables just prior to calling the generic view, so if your callable
creates a new queryset, it will be fresh each time.

All of the above is explained in the generic view documentation (last
paragraph in the introductory section). However there is evidence to
suggest that people don't read documentation with the "my life depends
on this" attention to detail I think should be compulsory.

Regards,
Malcolm


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

Reply via email to