-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathmodels.py
45 lines (35 loc) · 1.54 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from django.db import models
from django.core.validators import MinLengthValidator
from django.contrib.auth.models import User
from django.conf import settings
from taggit.managers import TaggableManager
class Forum(models.Model) :
title = models.CharField(
max_length=200,
validators=[MinLengthValidator(5, "Title must be greater than 5 characters")]
)
text = models.TextField()
# https://django-taggit.readthedocs.io/en/latest/api.html#TaggableManager
tags = TaggableManager(blank=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name='tagme_owner')
comments = models.ManyToManyField(settings.AUTH_USER_MODEL,
through='Comment', related_name='tagme_comments')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Shows up in the admin list
def __str__(self):
return self.title
class Comment(models.Model) :
text = models.TextField(
validators=[MinLengthValidator(3, "Comment must be greater than 3 characters")]
)
forum = models.ForeignKey(Forum, on_delete=models.CASCADE)
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name='tagme_comment_owner')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Shows up in the admin list
def __str__(self):
if len(self.text) < 15 : return self.text
return self.text[:11] + ' ...'