I am trying to build an app which will allow me to collect different types of media and use it with other apps. The models.py for this app looks like this:
from django.db import models as m
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from eventmanager.models import Concert
########################################################################################################################
# Prototypes #
########################################################################################################################
class BaseFile(m.Model):
""" """
class Meta:
abstract = True
# choices
AUDIO = 'a'
VIDEO = 'v'
IMAGE = 'i'
OTHER = 'o'
file_type_choices = (
(AUDIO, _('audio')),
(VIDEO, _('video')),
(IMAGE, _('image')),
(OTHER, _('other')),)
# properties
added_by = m.ForeignKey(User, null=True, default=None, on_delete=m.SET_NULL)
file_type = m.CharField(max_length=1, choices=file_type_choices)
size = m.IntegerField() # file size
extension = m.CharField(max_length=5) # file extension
downloadable = m.BooleanField(default=False)
class File(BaseFile):
""" """
class Meta:
abstract = True
# properties
internal = m.BooleanField()
file = m.FileField()
host = m.CharField(max_length=100, default='', blank=True)
url = m.URLField(blank=True)
class BaseImage(BaseFile):
""" """
class Meta:
abstract = True
# properties
image = m.ImageField()
class DataContainer(m.Model):
""" """
class Meta:
abstract = True
# choices
TRACK = 'tr'
LIVERECORDING = 'lr'
IMAGE = 'im'
FLYER = 'fl'
MUSICVIDEO = 'mv'
LIVEVIDEO = 'lv'
OTHER = 'ot'
media_type_choices = (
(TRACK, _('track')),
(LIVERECORDING, _('live recording')),
(IMAGE, _('image')),
(FLYER, _('flyer')),
(MUSICVIDEO, _('music video')),
(LIVEVIDEO, _('live video')),
(OTHER, _('other')),)
# properties
name = m.CharField(max_length=255)
media_type = m.CharField(max_length=2, choices=media_type_choices)
cr_date = m.DateTimeField(auto_now_add=True, editable=False, verbose_name=_('Date of Creation')) # date of creation
########################################################################################################################
# Single Items #
########################################################################################################################
class Image(BaseImage, DataContainer):
""" An image file with a one-to-one relationship with a caption. """
pass
class Track(File, DataContainer):
""" A music track. """
pass
class Video(File, DataContainer):
""" A video file. """
pass
class Flyer(BaseImage, DataContainer):
""" A concert flyer. """
concertmedia = m.ForeignKey('ConcertMedia', related_name='flyer')
########################################################################################################################
# Collections #
########################################################################################################################
class ConcertMedia(m.Model):
""" A collection of media associated with a concert: flyer, poster, photo gallery, audio recording, video recording,
etc... """
concert = m.ForeignKey(Concert, default=None, null=True, on_delete=m.SET_NULL)
Iām trying to figure out how to best connect different models to one another. Take for instance the ConcertMedia. Itās a container that has a relationship to concert which lives in the eventmanager app. A flyer and a poster would both be inhereting from image. So basically my question is this: What is a better way to provide the concert datacontainer with the flyer and the poster?
a. Create a submodel for flyer and one for poster inhereting from image. Give both of them a foreign key relationship to concert media.
b. Create a submodel for flyer and one for poster inhereting from image. Give image a foreign key relationship to concert media.
c. Only use the image model. Give the image model foreign key relationships to all the other models, where images will be used, for instance concert media, but also user avater, etcā¦
d. Only use the image model. Give the image model a GenericForeignKey. Define a relationship to image on all the models that I want to have images.
Which option do you prefer and why? Please bear in mind that I have I will have to add additional models to this class during initial development but also later on. So the structure should be easily extendable.
Is there any other ways to relate models to one another?