个人博客模型设计python3+django2
2019-06-26 本文已影响0人
中華小龍
在模型文件models.py中引入AbstractUser,用以继承Django自带用户模型
from django.db import models
from django.contrib.auth.models import AbstractUser
创建用户(User)模型,采用的继承方式扩展用户信息
class User(AbstractUser):
# 在继承的基础上新增4个字段
avatar = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default.png', max_length=200, blank=True, null=True, verbose_name='用户头像')
qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ号码')
mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')
url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')
# 使用内部的class Meta 定义模型的元数据
class Meta:
# verbose_name:数据库表名名称,这里表名称为“用户”
verbose_name = '用户'
# verbose_name_plural:人类可读的单复数名称,这里“用户”复数名称为“用户”
verbose_name_plural = verbose_name
# ordering:如排序选项,这里以id降序来排序
ordering = ['-id']
def __str__(self):
return self.username
创建分类(category)模型、文章(aticle)模型、评论(comment)模型
class Category(models.Model):
name = models.CharField(max_length=30, verbose_name='分类名称')
index = models.IntegerField(default=999, verbose_name='分类的排序')
class Meta:
verbose_name = '分类'
verbose_name_plural = verbose_name
ordering = ['index', 'id']
def __str__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=50, verbose_name='文章标题')
desc = models.CharField(max_length=50, verbose_name='文章描述')
content = models.TextField(verbose_name='文章内容')
click_count = models.IntegerField(default=0, verbose_name='点击次数')
is_recommend = models.BooleanField(default=False, verbose_name='是否推荐')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
user = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE)
category = models.ForeignKey(Category, blank=True, null=True, verbose_name='分类', on_delete=models.CASCADE)
class Meta:
verbose_name = '文章'
verbose_name_plural = verbose_name
ordering = ['-date_publish']
def __str__(self):
return self.title
class Comment(models.Model):
content = models.TextField(verbose_name='评论内容')
username = models.CharField(max_length=30, blank=True, null=True, verbose_name='用户名')
email = models.EmailField(max_length=50, blank=True, null=True, verbose_name='邮箱地址')
url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
user = models.ForeignKey(User, blank=True, null=True, verbose_name='用户', on_delete=models.CASCADE)
article = models.ForeignKey(Article, blank=True, null=True, verbose_name='文章', on_delete=models.CASCADE)
pid = models.ForeignKey('self', blank=True, null=True, verbose_name='父级评论', on_delete=models.CASCADE)
class Meta:
verbose_name = '评论'
verbose_name_plural = verbose_name
def __str__(self):
return str(self.id)
最后在admin.py里面注册模型到admin管理后台
from .models import User, Tag, Category, Article, Comment, Links, Ad
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
pass
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
pass
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
pass
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
pass
后使用以下命令,同步数据库,进入admin页面即可
python manage.py makemigrations
python manage.py migrate