Answer the question
In order to leave comments, you need to log in
How to display data from another model in django CVB ListView?
there are two models:
class ModelA(models.Model):
model_b = models.ForeignKey('ModelB')
ch_field0 = models.ImageField()
class ModelB(models.Model):
ch_field1 = models.CharField()
class BooksList(ListView):
model = ModelB
def get_context_data(self, **kwargs):
context = super(__class__, self).get_context_data(**kwargs)
context['mb'] = ModelB.objects.get(id=self.object.id).modela_set.all()
return context
Answer the question
In order to leave comments, you need to log in
Are you passing the context to the template?
So the necessary context already flies into the template:
{% for obj in objects_list %}
{% for img in obj.modela_set.all %}
{{ img }}
{% endfor %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question