|
|
from django.db import models
|
|
|
from ckeditor_uploader.fields import RichTextUploadingField
|
|
|
|
|
|
# Create your models here.
|
|
|
class Category(models.Model): #类别
|
|
|
cname = models.CharField(max_length=30,unique=True,verbose_name='类别名称')
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
db_table = 't_category'
|
|
|
verbose_name_plural='类别'
|
|
|
|
|
|
def __str__(self):
|
|
|
# 首先,检查self.cname是否为None或非字符串类型
|
|
|
if not isinstance(self.cname, str):
|
|
|
# 这里,您可以选择抛出一个具体的异常,或者返回一个默认值
|
|
|
# 例如,返回一个空字符串或者一个错误提示
|
|
|
raise ValueError("cname must be a string")
|
|
|
|
|
|
# 使用f-string进行字符串格式化
|
|
|
return f'Category: {self.cname}'
|
|
|
|
|
|
class Tag(models.Model): #标签
|
|
|
tname = models.CharField(max_length=30,unique=True,verbose_name='标签名称')
|
|
|
|
|
|
class Meta:
|
|
|
db_table = 't_tag'
|
|
|
verbose_name_plural = '标签'
|
|
|
|
|
|
def __str__(self):
|
|
|
# 首先,检查self.cname是否为None或非字符串类型
|
|
|
if not isinstance(self.tname, str):
|
|
|
# 这里,您可以选择抛出一个具体的异常,或者返回一个默认值
|
|
|
# 例如,返回一个空字符串或者一个错误提示
|
|
|
raise ValueError("cname must be a string")
|
|
|
|
|
|
# 使用f-string进行字符串格式化
|
|
|
return f'Tag: {self.tname}'
|
|
|
|
|
|
class Post(models.Model):
|
|
|
title = models.CharField(max_length=100,unique=True) #标题
|
|
|
desc = models.CharField(max_length=100) #简述
|
|
|
content = RichTextUploadingField(null=True,blank=True)
|
|
|
created = models.DateTimeField(auto_now_add=True) #发帖时间
|
|
|
category = models.ForeignKey(Category,on_delete=models.CASCADE) #类别
|
|
|
tag = models.ManyToManyField(Tag) #标签
|
|
|
|
|
|
class Meta:
|
|
|
db_table = 't_post'
|
|
|
verbose_name_plural = '帖子'
|
|
|
|
|
|
def __str__(self):
|
|
|
# 首先,检查self.cname是否为None或非字符串类型
|
|
|
if not isinstance(self.title, str):
|
|
|
# 这里,您可以选择抛出一个具体的异常,或者返回一个默认值
|
|
|
# 例如,返回一个空字符串或者一个错误提示
|
|
|
raise ValueError("cname must be a string")
|
|
|
|
|
|
# 使用f-string进行字符串格式化
|
|
|
return f'Post:{self.title}'
|
|
|
# 评论
|
|
|
class Comment(models.Model):
|
|
|
"""评论模型"""
|
|
|
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments', verbose_name='帖子')
|
|
|
content = models.TextField(verbose_name='内容')
|
|
|
created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
|
|
|
updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
|
|
|
|
|
|
class Meta:
|
|
|
db_table = 'comments'
|
|
|
verbose_name_plural = '评论'
|
|
|
ordering = ['-created_at']
|
|
|
|
|
|
def __str__(self):
|
|
|
return f'{self.content[:50]}'
|
|
|
|
|
|
|
|
|
|
|
|
|