Add multiple content to static page

Django -- Posted on Feb. 6, 2018

Add multiple content to static page

              
                import os
import datetime
from django.db import models


class Timestamped(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class UUSlugged(models.Model):
    slug = models.SlugField()

    class Meta:
        abstract = True


class Published(models.Model):
    is_published = models.BooleanField(default=False)

    class Meta:
        abstract = True


class Page(Timestamped, Published, UUSlugged):
    name = models.CharField(max_length=100)

    class Meta:
        default_related_name = 'pages'
        verbose_name = 'page'
        verbose_name_plural = 'pages'

    def __str__(self):
        return self.name


class Content(Timestamped, Published):
    page = models.ForeignKey(Page)
    name = models.CharField(max_length=100)
    content = models.TextField(blank=True)

    class Meta:
        default_related_name = 'content'
        verbose_name = 'content'
        verbose_name_plural = 'content'

    def __str__(self):
        return self.name


def get_upload_path(instance, filename):
    return os.path.join('{}/{}/{}/{}'.format(
        datetime.datetime.now().day,
        datetime.datetime.now().month,
        datetime.datetime.now().year, filename))


class ContentImage(Timestamped, Published):
    content = models.ForeignKey(Content)
    image = models.ImageField(
        upload_to=get_upload_path, null=True, blank=True)

    class Meta:
        default_related_name = 'contentimages'
        verbose_name = 'content image'
        verbose_name_plural = 'content images'
                  
   
            

Related Posts