I think then you will run into a problem with you add another media
type:

class WAVMedia(Media):
  file = models.CharField()
  def play(self):
    # do stuff

>>> m = Media()
>>> m.wavmedia = WAVMedia()
>>> m.mp3media = MP3Media()
>>> playlist = Playlist
>>> playlist.media_set.append(m)

for media in playlist.media_set.all():
  media.play()

Should media.play() call play() on the Media class, WAVMedia class, or
the MP3Media class? It *should* call it on the Media class, which it
does, since you have an instance of Media. And even you wanted it to
call the play method on MP3Media or WAVMedia, how would you determine
which one to call? But this is the way multiple table inheritance is
supposed to work. This is the way python works:

class Media(object):
    pass

class MP3Media(object):
    def play(self):
        # pass

If I have the following list:

songs = [ Media(), Media(), Media() ]

I would never expect to be able to call play() on any instance in that
list. Why would Django be any different?

I think in both these cases (there is no person actually an instance
of "Person", and there should be no actual instance of "Media"), it
would be best to mark the parent class abstract and use a generic
relation (like you said) to join to either MP3Media or WAVMedia. This
is the "Django way" and I believe is the functionality you're looking
for.

class Media(models.Model):
    filename = models.CharField(max_length=200)
    class Meta:
        abstract = True

class MP3Media(Media):
    def play(self):
      print "playing mp3"

    def __unicode__(self):
        return self.filename

class WAVMedia(Media):
    def play(self):
      print "playing wav"

    def __unicode__(self):
        return self.filename

class Playlist(models.Model):
    name = models.CharField(max_length=200)

class Song(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey() # WAVMedia or
MP3Media

    playlist = models.ForeignKey(Playlist)

>>> m = MP3Media(filename="test.mp3")
>>> w = WAVMedia(filename="test.wav")
>>> p = Playlist(name="My Songs")
>>> p.song_set.create(content_object=w)
>>> p.song_set.create(content_object=m)
>>> for song in p.song_set.all():
...     song.content_object.play()
playing mp3
playing wav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to