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?
{% if object.sub_object %}{{ object.sub_object }}{% endif %}