前期准备
配置
"""数据库配置"""
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bbs', #数据库名称
'USER': 'root', #用户名
'PASSWORD': '******', #密码
'HOST': '127.0.0.1',
'PORT': 3306,
'CHARSET': 'utf8'
}
}
"""静态文件配置"""
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static') #static是创建的文件夹
]
"""编写数据库用到继承AbstractUser类的如下代码时"""
from django.contrib.auth.models import AbstractUser
class Users(AbstractUser):
# 编写AbstractUser类中没有的字段 不能冲突!!!
phone = models.BigIntegerField()
addr = models.CharField(max_length=32)
#需配置:
UTH_USER_MODEL = 'app01.Users'
import pymysql
pymysql.install_as_MySQLdb()
数据库代码
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserInfo(AbstractUser):
"""用户表"""
phone = models.BigIntegerField(verbose_name="手机号", null=True)
avatar = models.FileField(verbose_name="头像", upload_to="avatar/", default="avatar/default.jpg")
"""给该字段传文件对象会保存到upload_to指定的位置 然后该字段存储该位置的路径"""
create_time = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)
blog = models.OneToOneField(to='Blog', null=True)
class Blog(models.Model):
"""个人站点表"""
site_name = models.CharField(verbose_name="站点名称", max_length=32)
site_title = models.CharField(verbose_name="站点标题", max_length=32)
site_style = models.CharField(verbose_name="站点样式", max_length=32)
class Article(models.Model):
"""文章表"""
title = models.CharField(verbose_name="文章标题", max_length=32)
desc = models.CharField(verbose_name="文章简介", max_length=255)
content = models.TextField(verbose_name="文章内容")
create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
# 优化字段
up_num = models.IntegerField(verbose_name="点赞数", default=0)
down_num = models.IntegerField(verbose_name="点踩数", default=0)
comment_num = models.IntegerField(verbose_name="评论数", default=0)
# 外键字段
blog = models.ForeignKey(to="Blog", null=True)
category = models.ForeignKey(to="Category", null=True)
tags = models.ManyToManyField(to="Tag", through="Article2Tag", through_fields=('article', 'tag'))
class Article2Tag(models.Model):
article = models.ForeignKey(to='Article')
tag = models.ForeignKey(to="Tag")
class Category(models.Model):
"""文章分类表"""
name = models.CharField(verbose_name="分类名称", max_length=32)
blog = models.ForeignKey(to="Blog", null=True)
class Tag(models.Model):
"""文章标签表"""
name = models.CharField(verbose_name="标签名称", max_length=32)
blog = models.ForeignKey(to="Blog", null=True)
class UpAndDown(models.Model):
"""点赞点踩表"""
user = models.ForeignKey(to="UserInfo", null=True)
article = models.ForeignKey(to="Article", null=True)
is_up = models.BooleanField(verbose_name="点赞点踩")
class Comment(models.Model):
"""评论表"""
user = models.ForeignKey(to="UserInfo")
article = models.ForeignKey(to="Article")
content = models.CharField(verbose_name="评论内容", max_length=255)
comment_time = models.DateTimeField(verbose_name="评论时间", auto_now_add=True)
# 自关联
parent = models.ForeignKey(to="self", null=True) # 有些评论就是根评论
makemigrations
migrate
标签:name,ForeignKey,models,BBS,系统,length,True,verbose
From: https://www.cnblogs.com/gjjcode/p/17234047.html