Nips
Nips

Reputation: 13860

How to get related model in template?

I have models:

class Model_1(models.Model):
    name = ...
    [...]

class Model_2(models.Model):
    model_1 = models.ForeignKey(Model_1)

and now I get model_1 objects in view, and in template I want to get model_2:

{% for m in models_1 %}
    {{ m.model_2 }} ?????
{% endfor %}

How to do it?

Upvotes: 3

Views: 1164

Answers (1)

Goin
Goin

Reputation: 3964

Try it:

{{ m.model_2_set.all }}

You can change model_2_set if you define the related name. There is not changes if this is in template or in a view

Upvotes: 8

Related Questions