设计表机构
from django.db import models # Create your models here. from django.contrib.auth.models import AbstractUser,User class UserInfo(AbstractUser): nid=models.AutoField(primary_key=True) telephone=models.CharField(max_length=11,null=True,unique=True) avatar=models.FileField(upload_to='avatars/',default='/avatars/default.png')#用户头像 create_time=models.DateTimeField(verbose_name='创建时间',auto_now_add=True)#用户注册时间,生成表记录时自动添加 blog=models.OneToOneField(to='Blog',to_field='nid',null=True,unique=True,on_delete='CASCADE') def __str__(self): return self.username class Blog(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(verbose_name="个人博客标题",max_length=64) site_name=models.CharField(verbose_name="站点名称",max_length=64) theme=models.CharField(verbose_name='博客主题样式',max_length=32) def __str__(self): return self.title #分类表 class Category(models.Model): nid = models.AutoField(primary_key=True) title=models.CharField(verbose_name="分类标题",max_length=32) blog=models.ForeignKey(verbose_name="所属博客",to="Blog",to_field="nid",on_delete='CASCADE') def __str__(self): return self.title #标签表 class Tag(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(verbose_name="标签名称",max_length=32) blog=models.ForeignKey(verbose_name='所属博客',to="Blog",to_field='nid',on_delete='CASCADE') def __str__(self): return self.title #文章表 class Article(models.Model): nid = models.AutoField(primary_key=True) title=models.CharField(verbose_name='文章标题',max_length=50) desc=models.CharField(verbose_name='文章描述',max_length=128) create_time=models.DateTimeField(verbose_name="文章创建时间",auto_now_add=True) content = models.TextField() comment_count=models.IntegerField(default=0) up_count=models.IntegerField(default=0) down_count=models.IntegerField(default=0) user=models.ForeignKey(verbose_name='作者',to="UserInfo",to_field='nid',on_delete='CASCADE') category=models.ForeignKey(verbose_name='文章分类',to='Category',to_field='nid',null=True,on_delete='CASCADE') tags=models.ManyToManyField(to='Tag',through='Atricle2Tag',through_fields=('article','tag')) def __str__(self): return self.title class Atricle2Tag(models.Model): nid=models.AutoField(primary_key=True) article=models.ForeignKey(verbose_name='文章',to='Article',to_field='nid',on_delete='CASCADE') tag=models.ForeignKey(verbose_name='标签',to='Tag',to_field='nid',on_delete='CASCADE') class Meta: #联合唯一,也可以理解为联合主键 unique_together=[('article','tag'),] def __str__(self): v=self.article.title+'---'+self.tag.title return v #点赞表 class ArticleUpDown(models.Model): nid=models.AutoField(primary_key=True) user=models.ForeignKey(to='UserInfo',to_field='nid',null=True,on_delete='CASCADE') article=models.ForeignKey(to='Article',to_field='nid',null=True,on_delete='CASCADE') is_up=models.BooleanField(default=True) class Meta: #联合唯一,一张表同一个用户只能点赞一次 unique_together=[('article','user'),] #评论表 class Comment(models.Model): nid=models.AutoField(primary_key=True) article=models.ForeignKey(verbose_name="评论文章",to='Article',to_field='nid',on_delete='CASCADE') user=models.ForeignKey(verbose_name="评论用户",to='UserInfo',to_field='nid',on_delete='CASCADE') content=models.CharField(verbose_name="评论内容",max_length=128) create_time=models.DateTimeField(verbose_name='创建时间',auto_now_add=True) parent_comment=models.ForeignKey(to='Comment',to_field='nid',null=True,on_delete='CASCADE') # parent_comment=models.ForeignKey(to='self',null=True) def __str__(self): return self.content
数据库迁移
配置mysql数据库
DATABASES={ 'default':{ 'ENGINE':'django.db.backends.mysql', 'NAME':'cnblog', 'USER':'root', 'PASSWORD':'123.com', 'HOST':'127.0.0.1', 'PORT':3306 } }
配置AUTH_USER_MODEL
因为我们使用的是自己的UserInfo并继承于AbstractUser类进行认证,所以要告知django
AUTH_USER_MODEL='app.UserInfo'
迁移数据库
python manage.py makemigrations
python manage.py migrate