I am trying to create a song-artist-album relationship in Django. I have the following models:
class Artist(models.Model):
gid = models.CharField(max_length=63, blank=True)
name = models.CharField(max_length=255, blank=True)
begin_life = models.CharField(max_length=31, blank=True)
end_life = models.CharField(max_length=31, blank=True)
type = models.CharField(max_length=1, blank=True)
gender = models.CharField(max_length=1, blank=True)
class Song(models.Model):
gid = models.CharField(max_length=63, blank=True)
title = models.CharField(max_length=255, blank=True)
artist = models.ForeignKey('Artist', related_name='songs_artist')
album = models.ForeignKey('Album', related_name='songs_album')
length = models.IntegerField(default=0)
I have created my ArtistSerializer so that I can retrieve all the songs of the artist when I get the info of any particular artist. This is the serializer I have created:
class ArtistSerializer(serializers.ModelSerializer):
songs_artist = SongSerializer(source='songs_artist')
class Meta:
model = Artist
fields = ('name', 'type', 'gender', 'begin_life', 'end_life', 'songs_artist')
class SongSerializer(serializers.ModelSerializer):
artist = SongArtistSerializer()
album = SongAlbumSerializer()
class Meta:
model = Song
fields = ('id', 'title', 'artist', 'album', 'length')
class SongArtistSerializer(serializers.ModelSerializer):
class Meta:
model = Artist
fields = ('id', 'name')
A quick profiling on the GET method of my artist revealed some troubling facts. Following are the results of the profiling ordered by time and number of calls: http://pastebin.com/bwcKsn2i.
But, when I removed the songs_artist
field from my serializer, following was the output of the profiler: http://pastebin.com/0s5k4w7i.
If I read right, the database is being hit 1240 times when I use source
!
Is there any other alternative to doing this?
Thanks in advance.