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)
posts / models.py
class Post(models.Model):
...
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?
source
share