Answer the question
In order to leave comments, you need to log in
Rendering a model with associated data - a newbie Django question?
There are two models - the first carries information about music albums, the second describes tracks from albums:
<font color="black"><a href="http://s-c.me/19881/s">Copy Source</a> | <a href="http://s-c.me/19881/h">Copy HTML</a><ol> <li><font color="#0000ff">class</font> <font color="#cc6633">Album</font>(models.Model):</li> <li> id_album = models.AutoField(primary_key=True)</li> <li> artist = models.CharField(max_length=<font color="#008000">100</font>, null=True)</li> <li> disc = models.CharField(max_length=<font color="#008000">100</font>, null=True)</li> <li> </li> <li><font color="#0000ff">class</font> <font color="#cc6633">Track</font>(models.Model):</li> <li> id_track = models.AutoField(primary_key=True)</li> <li> id_album = models.ForeignKey(<font color="#cc6633">Album</font>)</li> <li> title = models.CharField(max_length=<font color="#008000">100</font>) </li> </ol></font>
<font color="black"><a href="http://s-c.me/19882/s">Copy Source</a> | <a href="http://s-c.me/19882/h">Copy HTML</a><ol> <li><font color="#0000ff">def</font> <font color="#cc6633">albums</font>(request):</li> <li> <font color="#cc6633">albums</font> = Album.objects.order_by(<font color="#008000">'id_album'</font>).<b>all</b>()[<font color="#008000"> 0</font>:<font color="#008000">10</font>]</li> <li> <font color="#0000ff">return</font> render_to_response(<font color="#008000">'albums.html'</font>, {<font color="#008000">'albums'</font>: <font color="#cc6633">albums</font>}) </li> </ol></font>
Answer the question
In order to leave comments, you need to log in
Although not a specialist, I myself am only mastering jangu
<ul>
{% for album in albums %}
<li>{{ album.artist }} - {{album.disc}}
<ol>
{% for track in album.track_set.all %}
<li>{{ track.title }}</li>
{% endfor %}
</ol>
</li>
{% endfor %}
albums = Album.objects.select_related().order_by('id_album').all()[ 0:10]
class Album(models.Model):
artist = models.CharField(max_length=100, null=True)
disc = models.CharField(max_length=100, null=True)
class Track(models.Model):
album = models.ForeignKey(Album)
title = models.CharField(max_length=100)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question