[Django]-Query multiple models with class-based views

6👍

✅

This isn’t quite a DetailView as you have multiple objects, but it isn’t a ListView either, nor does it look like a FormView or its children.

Since you gain nothing from those, a simple TemplateView is probably the way to go.

2👍

If you are querying the same UserInfo, Purchases, Favorites, etc in multiple views, create a Mixin that you can re-use.

class CommonUserInfoMixin (object):
    def get_context_data(self, **kwargs):
        context = super(OrgContextMixin, self).get_context_data(**kwargs)
        ... # Add more to context object

Then you can use this in your normal List, Detail, Update, etc CBV’s

class ItemList(CommonUserInfoMixin, ListView):
    ....

Leave a comment