Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4585628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:31:22+00:00 2026-05-21T21:31:22+00:00

I have a situation I don´t know how to model correctly. I want every

  • 0

I have a situation I don´t know how to model correctly. I want every child of a class to be associated with a media object (photo, video or music). I want to know which is the preffered approach to this problem. What I have right now is:

class Something(models.Model):

    media = models.ForeignKey(Media)


class Media(models.Model):
    title = models.CharField(max_lenght=100)
    def get_tiny_object():
        pass
    def get_big_object():
        pass

class Picture(Media):
    picture = models.ImageField()
    def get_tiny_object():
        return ...
    ...

class Video(Media):
    video = models.CharField(max_length=200) #youtube id
    ...

class Music(Media):
    music = ....

You get the idea. ¿Does this work? Should I also record on “Something” what kind of media it is?

EDIT:

The idea behind having a Media class, is that I can render in the templates without knowing which kind of media I´m rendering. get_tiny_object() should return, if it is a picture:

"<img style="width:60px; height: 50px" ...>"

So if I have a foreign key to a media object lets say it’s id=4, does django know that it should be fetched from music table, if the object I associated with is of Music kind? Because I´ll have 3 different id=4, one on each table (picture, video and music (and possibly more if the domain changes)).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:31:22+00:00Added an answer on May 21, 2026 at 9:31 pm

    I still think the question is a little hard to understand – the title is Database modelling in Django after all…However, this is a perfectly valid and reasonable thing to do, and seems to fit your intent:

    The recommended way to do this is multi table inheritance – example:

    class Media(models.Model):
        pass
    
    class Something(models.Model):
        media = models.ForeignKey(Media)
    
    class Picture(Media):
        foo = models.CharField(max_length=100)
    
        def get_tiny_object(self):
            return self.foo
    
    class Video(Media):
        bar = models.CharField(max_length=100)
    
        def get_tiny_object(self):
            return self.bar
    
    picture = Picture.objects.create(foo='some picture')
    video = Video.objects.create(bar='some video')
    something1 = Something.objects.create(media=picture)
    something2 = Something.objects.create(media=video)
    print something1.media.get_tiny_object() # this is a picture remember?
    print something2.media.get_tiny_object() # and lo, here is a video
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if you have run into a similar situation but here's a
i have situation like this: class IData { virtual void get() = 0; virtual
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have following situation: class TextBoxCellControl : TextBox, IDataGridViewCellControl class EnhancedTextBoxCellControl : Panel, IDataGridViewCell
Here's the situation: Let's say I have a model A in django. When I'm
Here's my situation.. Suppose you have the following model entities, which represent single tables
I have a Posts model and every post also contains Blocks (also a model).
I know questions of this kind have been asked before, but my situation differs
I have a NSDictionary and I don't know how I can retrieve the value
I wanted to have more than one controller and view for same object/model in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.