在做任何项目之前,首先需要对此项目做一个分析和功能构造,所以对数据库的创建则为重中之重,因为一些功能都是由数据库进行构建和开发的,所以对数据库的构造是很重要的,那么,关于要开发一个BBS论坛,那么其数据库也好好好地规划一下!
下面则是关于BBS数据库的相关代码!
from django.db import models from django.contrib.auth.models import User from django.core.exceptions import ValidationError import datetime # Create your models here. class Article(models.Model): # 文章 title = models.CharField(max_length=255) # 标题 brief = models.CharField(null=True,blank=True,max_length=255) # 摘要 category = models.ForeignKey("Category", on_delete=models.CASCADE) # 板块 content = models.TextField(u"文章内容") # 文章内容 author = models.ForeignKey("UserProfile", on_delete=models.CASCADE) # 作者 pub_date = models.DateTimeField(blank=None) # auto_now_add = True 默认不能更改的 last_modify = models.DateTimeField(auto_now=True) priority = models.IntegerField(u"优先级",default=1000) # 优先级 head_img = models.ImageField(u‘文章标题图片‘,upload_to=‘img‘) status_choices = (("draft",u"草稿"), ("published",u"已发布"), ("hidden","隐藏")) status = models.CharField(choices=status_choices,default="published",max_length=255) def __dir__(self): return self.title def clean(self): if self.status == "draft" and self.pub_date is not None: raise ValidationError((‘Drafe entries may not have a publication data‘)) if self.status == "published" and self.pub_date is not None: self.pub_date = datetime.date.today() class Comment(models.Model): # 评论 或 点赞 article = models.ForeignKey(Article,verbose_name=u"所属文章", on_delete=models.CASCADE) parent_comment = models.ForeignKey(‘self‘,related_name=‘my_children‘, on_delete=models.CASCADE) # 父评论有多少子评论 comment_choices = ((1,u"评论"), (2,u"点赞")) comment_type = models.IntegerField(choices=comment_choices,default=1) user = models.ForeignKey("UserProfile", on_delete=models.CASCADE) comment = models.TextField(blank=True,null=True) # date = models.DateTimeField(auto_now_add=True) def clean(self): if self.comment_type == 1 and len(self.comment) == 0: raise ValidationError(u"评论不能为空") def __dir__(self): return "%s,P:%s,%s"%(self.article.id,self.parent_comment,self.comment) class Category(models.Model): # 板块 name = models.CharField(max_length=255,unique=True) brief = models.CharField(null=True,blank=True,max_length=255) # @@ set_as_top_menu = models.BooleanField(default=False) # 设置动态的菜单 position_index = models.SmallIntegerField() # 板块的序号 admins = models.ManyToManyField("UserProfile",blank=True) # 版主 def __dir__(self): return self.name class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) name = models.CharField(max_length=32) signature = models.CharField(null=True, blank=True, max_length=255) # 个人签名 head_img = models.ImageField(height_field=150,width_field=150,null=True, blank=True) def __dir__(self): return self.name
为了使其看的更方便,这里我使用了Navicat for MySQL这款软件,来看看存储数据的数据库构造,如下图。
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/9419556.html
时间: 2024-10-07 18:09:16