I’m trying to model my app and I’ve come across this problem:
I have a Slides which can be images, text or videos.
Also, images and videos can be in many different slides and presentations of the same user.
I also want to store which presentation the slide belongs to and the insert date (slides must have an order).
class Slide(models.Model):
user = models.ForeignKey(User)
presentation = models.ForeignKey(Presentation)
insert_date = models.DateTimeField(auto_now_add=True)
–
class Image(models.Model):
def get_image_path(instance, filename):
return os.path.join('users/images', str(instance.id), filename)
user = models.ForeignKey(User)
slide = models.ManyToManyField(Slide)
image = models.ImageField(upload_to=get_image_path)
insert_date = models.DateTimeField(, auto_now_add=True)
–
class SlideText(models.Model):
slide = models.OneToOneField(Slide)
text = models.TextField()
[Video model will be similar to Image]
Problems with this model are that a Slide can be empty, and that a slide can contain both Images and Texts (as well as Video) and it shouldn’t.
I can image that this would be a common problem: certain Item have some information related depeding on the “type of Item” (that’d be Slide of image, Slide of text…), so what’s the common solution ?
any help really appreciated! thanks!
Generic relations are what you want.