Common Relationship Troubleshooting in django

I want to simulate a situation, and I had problems processing it. The domain is as follows: there are messages, and each message must be connected to one another using MediaContent. MediaContent can be an image or video (for now, maybe later). So, I have a:

mediacontents / models.py

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

posts / models.py

class Post(models.Model):
    ...
    # link to MediaContent
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    media_content = generic.GenericForeignKey('content_type', 'object_id')

What I ultimately want to do cannot call methods such as:

post1.media_content.draw_item()
>> <iframe src="youtube.com" ...>
post2.media_content.draw_item()
>> <img src="..."/>

Is this the correct aproach, does it work? Can a template be an agnostic of an object under?

+1
source share
1 answer

. draw_item Picture Video. :

{% for post in posts %}
  {{ post.media_content.draw_item }}
{% endfor %}

, , draw_item.

+1

All Articles