Skip to content

Instantly share code, notes, and snippets.

@vitorfs
Created October 8, 2017 17:02
Show Gist options
  • Save vitorfs/caa24fcf2b66903617ebbb41337d3d3d to your computer and use it in GitHub Desktop.
Save vitorfs/caa24fcf2b66903617ebbb41337d3d3d to your computer and use it in GitHub Desktop.
from django.contrib.auth.models import User
from django.db import models
from django.utils.html import mark_safe
from django.utils.text import Truncator
from markdown import markdown
class Board(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
def __str__(self):
return self.name
def get_posts_count(self):
return Post.objects.filter(topic__board=self).count()
def get_last_post(self):
return Post.objects.filter(topic__board=self).order_by('-created_at').first()
class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, related_name='topics')
starter = models.ForeignKey(User, related_name='topics')
views = models.PositiveIntegerField(default=0)
def __str__(self):
return self.subject
class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, related_name='posts')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(User, related_name='posts')
updated_by = models.ForeignKey(User, null=True, related_name='+')
def __str__(self):
truncated_message = Truncator(self.message)
return truncated_message.chars(30)
def get_message_as_markdown(self):
return mark_safe(markdown(self.message, safe_mode='escape'))
@muwanguzipoloe
Copy link

In case anyone is using python 3.6.5 and you get the following error after making changes:

TypeError: init() missing 1 required positional argument: 'on_delete'

Just make sure you add **on_delete=models.DO_NOTHING ** or on_delete=models.CASCADE
to the lines that are being flagged in your terminal, it should be the ones with the models.foreignKeys .

for example:

class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, related_name='topics', on_delete=models.DO_NOTHING)
starter = models.ForeignKey(User, related_name='topics', on_delete=models.DO_NOTHING)
views = models.PositiveIntegerField(default=0)

def __str__(self):
    return self.subject

class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, related_name='posts', on_delete=models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(User, related_name='posts', on_delete=models.DO_NOTHING)
updated_by = models.ForeignKey(User, null=True, related_name='+',on_delete=models.CASCADE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment