I created tree-based model of categories with django-mptt and added a few test items to them. I use templates and can display on one page categories or items. Can i display categories and associated items on one page? For example, I made some drawings about the situation.
I understand this, but I can’t find how to do it. I tried to merge the two views by creating a third one. After that, I made new template where i called that view. But that didn’t work.
Maybe you know an easy way and can give me a hint?
You already know how to retrieve data from the database, create a context from that data, then render a result.
Why don’t you post your attempt and we can work forward from there?
My only suggestion for this is that you start simple. If you’re wrestling with the idea of rendering two different querysets in the same view, then pick two different simple querysets to start with.
Also, you haven’t mentioned whether you’re trying to do this in an FBV or a CBV. I’ll take this opportunity to point out that the Django-provided generic CBVs are heavily oriented toward representations of single querysets only. If you’re working with multiple querysets, I wouldn’t suggest trying to build your view on anything more than the generic View class (or more likely, TemplateView). The more sophisticated views such as ListView, UpdateView, etc, are not necessarily the best “fit” for something like this if you’re wanting to do anything other than just displaying the data from those other models.
Actually, having both is quite appropriate in many circumstances. Their intent is to satisfy two separate and distinct functional requirements within the CBV.
Every “higher level” CBV (e.g. ListView, CreateView, UpdateView, DetailView) is designed around the idea of handling one Model (ListView) or Object (the others).
The purpose of get_queryset and get_object is to retrieve the data related to that entity.
The purpose of get_context_data is to retrieve all other data you may wish to use in those views.