V
V
Vladislav Ivanov2018-06-20 10:45:39
Django
Vladislav Ivanov, 2018-06-20 10:45:39

How to output model data to Django template?

Good afternoon. I'm new to django, so I'm asking for help from the experts.
There are 2 models

class Musican(models.Model):
    band = models.CharField(max_length=10,null=True)
    albums = models.CharField(max_length=100)

    def __str__(self):
        """
        String for representing the Model object.
        """
        return self.albums

class Soung(models.Model):
    albums = models.ForeignKey(Musican,on_delete=models.CASCADE)
    soungs = models.CharField(max_length=100)


    def __str__(self):
        return self.soungs

The bottom line is, the first table is a group and an album, the second is the compositions of a certain album. So I can’t figure out how to display in the template so that clicking on an album opens a list of songs for this album

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max Cage, 2018-06-20
@zulune

As an option in the Musican model, write a function:

@property
def get_soung(self):
   instance = self
   qs = Soung.objects.filter_by_instance(instance)
   return qs

In template:
{% for music in instance.get_soung %}
  {{ music.soungs }}
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question