May 27th, 2009 posted by Bender Rodríguez
Given the models below:
class Artist(models.Model):
name = models.CharField(max_length=128)
def __unicode__(self):
return u'%s' %(self.name)
class Album(models.Model):
title = models.CharField(max_length=128)
artists = models.ManyToManyField(Artist)
def __unicode__(self):
return u'%s' %(self.title)
class Song(models.Model):
title = models.CharField(max_length=128)
albums = models.ManyToManyField(Album)
def __unicode__(self):
return self.title
Here is how you can get a list of all the songs a particular artist has:
Song.objects.filter(albums__artists=
"No fathers or mothers think their own children ugly; and this self-deceit is yet stronger with respect to the offspring of the mind."
Miguel de Cervantes