To start, let's create the viral_videos app and add it under INSTALLED_APPS:
# settings.py or conf/base.py INSTALLED_APPS = ( # ... # local apps "utils", "viral_videos", )
Next, create a model for viral videos, with creation and modification timestamps, a title, embedded code, impressions by anonymous users, and impressions by authenticated users, as follows:
# viral_videos/models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
from utils.models import CreationModificationDateMixin, UrlMixin
class ViralVideo(CreationModificationDateMixin, UrlMixin):
class Meta:
verbose_name = _("Viral video")
verbose_name_plural = _("Viral videos")
title = models.CharField(
_("Title"),
max_length=200,
blank=True)
embed_code = models.TextField(
_("YouTube embed code"),
blank=True)
anonymous_views = models.PositiveIntegerField(
_("Anonymous impressions"),
default=0)
authenticated_views = models.PositiveIntegerField(
_("Authenticated impressions"),
default=0)
def get_url_path(self):
from django.urls import reverse
return reverse("viral-video-detail",
kwargs={"id": str(self.id)})
def __str__(self):
return self.title
Be sure to make and run migrations for the new app, so that your database will be ready to go:
(myproject_env)$ python3 manage.py makemigrations viral_videos
(myproject_env)$ python3 manage.py migrate viral_videos