is there a way to combine generic views?

31 views
Skip to first unread message

Anton Daneika

unread,
Dec 7, 2006, 10:23:21 AM12/7/06
to django...@googlegroups.com
Greetings, django users.

I want a page which displays both the details of an object and the list of the objects. Is there a known way to combine generic views to produce such a thing? The problem is that all generic views return HttpResponse which I don't need in that case -- just the processing.
I suppose it's just not the use case of the generic views -- they are not for this.

Anton

Adrian Holovaty

unread,
Dec 7, 2006, 10:37:40 AM12/7/06
to django...@googlegroups.com

Hi Anton,

You suppose correctly. :) None of the generic views combine an "object
detail" with "object list."

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Jay Parlar

unread,
Dec 7, 2006, 12:55:50 PM12/7/06
to django...@googlegroups.com
On 12/7/06, Adrian Holovaty <holo...@gmail.com> wrote:
>
> On 12/7/06, Anton Daneika <mez...@gmail.com> wrote:
> > I want a page which displays both the details of an object and the list of
> > the objects. Is there a known way to combine generic views to produce such a
> > thing? The problem is that all generic views return HttpResponse which I
> > don't need in that case -- just the processing.
> > I suppose it's just not the use case of the generic views -- they are not
> > for this.
>
> Hi Anton,
>
> You suppose correctly. :) None of the generic views combine an "object
> detail" with "object list."

But it's very easy to pass in the object list as 'extra_context' to
the generic view, which I think people sometimes forget about.

Jay P.

Condredge

unread,
Dec 7, 2006, 1:30:43 PM12/7/06
to Django users
Hey Anton,
Another option is to write a wrapper view that makes use of the generic
views but adds more to it. James Bennett over at B-List wrote a very
useful article on this:
http://www.b-list.org/weblog/2006/11/16/django-tips-get-most-out-generic-views

Terji7

unread,
Dec 7, 2006, 8:16:13 PM12/7/06
to Django users

In my opinion this is a must-know w.r.t. generic views.

Actually, IMO this should be the standard and recommended way of using
generic views instead of putting them into url.py. it is just cleaner
and more logical to keep all views (also generic ones) in the view.py
file.

regards
Terri

machineghost

unread,
Dec 9, 2006, 1:26:18 PM12/9/06
to Django users
>> I want a page which displays both the details of an object and the list of
>> the objects.
Really important question: is the object you want the details of
related to the list of objects, via a ForeignKey or ManyToManyField?
Because if so, you don't need to do any view combination at all. (Of
course, if they are unrelated, everything everyone else said about
extending generic views is dead on).

One thing that no one here has mentioned, and which the documentation
also does a poor job of mentioning (presumably because it explains
models before it explains templates, and thus doesn't want to get in to
template details when it explains model realtionships) is that if you
define relationships between objects, you can access related objects in
templates. For instance, let's say you have these models:
class UL(models.Model):
pass
class LI(models.Model):
UL = models.ForeignKey(UL)
name = models.TextField()
Using the object detail generic view on UL will give you an "object"
variable in your template for the given UL. This variable will have an
attribute (my terminology may be a little off here, but I'm too lazy to
look up the correct phrase, so just look at my examples if you're
confused): "li_set". This attribute in turn has two attributes: "all",
and "count". You can use these attributes to access the related LI's.
For instance, using:
{{ object.li_set.count }}
in your template will return the number of LI objects that have a
foreign key of this particular UL object's ID. Similarly, using:
{% for li in object.li_set.all %}
{{ li.name }}
{% endfor %}
will return the names of all of the LI's associated with this
particular UL object.

Hope that helps.

Jeremy

Reply all
Reply to author
Forward
0 new messages