Cyril2014-04-27 12:53:00
Cyril, 2014-04-27 12:53:00

How to make feedback for OneToMany in templates in Django?

I ran into a wall, I've been suffering for 2 days, help me out!
There are 2 models:
class Universe(models.Model):
name = models.CharField(max_length=50, unique=True)
lore = models.TextField(max_length=200)
class Achievement(models.Model):
uni = models.ForeignKey( Universe)
name = models.CharField(max_length=50)
There is a view in views.py:
class UniverseDetailView(DetailView):
model = Universe
and a template for it universe_detail.html
{% block content %}
Universe: {{ universe.name }}
History: {{ universe.lore }}
{% endblock %}
How can I get a list of achievements from this universe? Something like
{% for Achievement in universe.Achievement.all %} For the ManyToMany connection, this is exactly how it worked for me ...
Maybe I need to create a separate form for this? Or add a function to the Universe model?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
DevMan, 2015-10-14

if the line contains only the description of the function (without the body, as in the example), then you can https://regex101.com/r/iA6pL8/1

Nadz Goldman, 2015-10-14

Use ^ , $ and +

Cyril, 2014-04-27

kind person answered stackoverflow:
{% for achievement in universe.achievement_set.all %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question