How to Construct a View with multiple and singular objects?

29 views
Skip to first unread message

Malik Rumi

unread,
Aug 4, 2014, 11:27:00 AM8/4/14
to django...@googlegroups.com

I have a model with numerous objects. However, some of these objects have sub-objects and some do not. When calling up an object with sub-objects, (linked by a foreign key) I’d like to display

Object

1.    Sub object

2.    Sub object

And when the user requests an object with no subs, they get

Object

However, if I have a ‘get’ for objects in my view, I am going to get an error for all objects that have sub objects.

Similarly, if I use a listview, I will get all the objects and sub objects on one long page, which I don’t want.

My tentative solution is to put this in the template:

If objects with sub objects

{{List of linked sub objects}}

else

{{object}}

Assuming that works, my difficulty is with the view. How do I write it to cover both situations without putting them in separate models?

Collin Anderson

unread,
Aug 4, 2014, 12:43:52 PM8/4/14
to django...@googlegroups.com
Do you get an error if you wrap it in an if statement?

{% if object.sub_object %}{{ object.sub_object }}{% endif %}

Malik Rumi

unread,
Aug 11, 2014, 9:28:20 AM8/11/14
to django...@googlegroups.com
This is on hold while I wrestle with other problems. 
Reply all
Reply to author
Forward
0 new messages