Skip to content

Instantly share code, notes, and snippets.

@lewis-kori
Created September 15, 2020 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewis-kori/0c8f81397b1b0fc7a059c3f17a8e2bcf to your computer and use it in GitHub Desktop.
Save lewis-kori/0c8f81397b1b0fc7a059c3f17a8e2bcf to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.
class CommonInfo(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
ordering = ('-created_at',)
# blog post category
class Category(CommonInfo):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
# blog post instance
class Post(CommonInfo):
title = models.CharField(max_length=255)
category = models.ForeignKey(Category,related_name='posts',on_delete=models.PROTECT)
author = models.ForeignKey(User,related_name='posts',on_delete=models.PROTECT)
content = models.TextField()
published = models.BooleanField(default=False)
def __str__(self):
return f'{self.title} by {self.author.get_full_name()}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment