R
R
rhost2015-09-27 22:16:12
Python
rhost, 2015-09-27 22:16:12

How to display model data?

There are two models

class TypeSkills(models.Model):
    type = models.CharField('Тип', max_length=100)
class Skill(models.Model):
    type = models.ForeignKey(TypeSkills, verbose_name='Тип')
    name = models.CharField('Навык', max_length=100)

in the view I take all "Type"
types = TypeSkills.objects.all()
But how to get "skills" by type I can't understand ... I just haven't tried it.
the result should be
type:
skill, skill
type:
skill, skill

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2015-09-28
@hoOstel

for typeSkill in TypeSkills.objects.all():
    print typeSkill
    print typeSkill.skill_set.all()

If in template:
{% for type in types %}
    {{ type }}: <br>
    {% for skill in type.skill_set.all %}
        {{ skill }}
    {% endfor %}
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question