Creating models for polymorphic content
You plan to add different types of content to the course modules, such as text, images, files, and videos. Polymorphism is the provision of a single interface to entities of different types. You need a versatile data model that allows you to store diverse content that is accessible through a single interface. In Chapter 7, Tracking User Actions, you learned about the convenience of using generic relations to create foreign keys that can point to the objects of any model. You are going to create a Content
model that represents the modules’ contents and define a generic relation to associate any object with the content object.
Edit the models.py
file of the courses
application and add the following imports:
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
Then, add the following code to the end of the file:
class Content(models.Model):
module...