-
-
Save vitorfs/74077336decd75292082752eb8405ad3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
from django.db import models | |
from django.utils.text import Truncator | |
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') | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment