2👍
✅
You can pass the list directly in the context. If you are using class-based generic views, pass an
def get_context_data(self, **kwargs):
context_data = super(EntryView, self).get_context_data(**kwargs)
related_entries = Entry.objects.filter(
tags__name__in=list(self.object.tags.values_list('name', flat=True))
).exclude(id=self.object.id)
context_data['related_entries'] = related_entries
return context_data
https://docs.djangoproject.com/en/dev/topics/class-based-views/#adding-extra-context
You should add this only in views where you really need it, and tailor to what you need to display, not blindly add this in the context of any view whatever the view is.
Source:stackexchange.com