3

In this SO question I see the following:

class MediaContent(models.Model):
    uploader = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)

    def draw_item(self):
        pass

    class Meta:
        abstract = True

class Picture(MediaContent):
    picture = models.ImageField(upload_to='pictures')

class Video(MediaContent):
    identifier = models.CharField(max_length=30) #youtube id

I have done some STI in Rails before, but never in django. Is this how it's done in django? Will it only create one table having all the fields in all the models? Will it add a type column?

Community
  • 1
  • 1
Geo
  • 93,257
  • 117
  • 344
  • 520
  • The acronym 'STI' isn't widely used in the Django world (and appears to be largely a Rails-ism). It would have been clearer if you'd spelt it out in full in your question title. – Andy Baker Jun 09 '11 at 08:51
  • Sorry, I spelled it out full in the tag. I thought it would be enough. I will edit the title. – Geo Jun 09 '11 at 09:28

2 Answers2

4

Unfortunately, Django does not support single table inheritance: Single Table Inheritance in Django

Community
  • 1
  • 1
Juande Carrion
  • 750
  • 8
  • 11
3

There will be two tables created, one for Picture, and one for Video. It will not be possible to create a query that returns both types.

"Abstract base classes"

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358