搭建blog需要的一些表

from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.
class UserInfo(AbstractUser):
    '''
    用户信息表
    '''
    nid = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=11, null=True, unique=True)
    avatar = models.FileField(upload_to='avatars/', default='avatars/default.png', verbose_name='头像')
    create_time = models.DateTimeField(auto_now_add=True)

    blog = models.OneToOneField(to='Blog', to_field='nid', null=True)

    def __str__(self):
        return self.username


class Blog(models.Model):
    '''
    博客信息表
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, )  # 个人博客标题
    site = models.CharField(max_length=32, unique=True)  # 个人博客后缀
    theme = models.CharField(max_length=32)  # 个人博客主题

    def __str__(self):
        return self.title


class Category(models.Model):
    '''
    博客分类表
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    blog = models.ForeignKey(to='Blog', to_field='nid')

    def __str__(self):
        return self.title


class Tag(models.Model):
    '''
    博客标签表
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    blog = models.ForeignKey(to='Blog', to_field='nid')

    def __str__(self):
        return self.title


class Article(models.Model):
    '''
    文章表
    '''
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50)
    desc = models.CharField(max_length=256)  # 文章描述
    create_time = models.DateTimeField(auto_now_add=True)

    category = models.ForeignKey(to='Category', to_field='nid', null=True)
    user = models.ForeignKey(to='UserInfo', to_field='nid')
    tag = models.ManyToManyField(
        to='Tag',
        through='Article2Tag',
        through_fields=('article', 'tag'),
    )

    def __str__(self):
        return self.title


class Article2Tag(models.Model):
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(to='Article', to_field='nid')
    tag = models.ForeignKey(to='Tag', to_field='nid')

    class Meta:
        unique_together = (('article', 'tag'),)  # 联合唯一索引


class ArticleDetail(models.Model):
    '''
    文章详情表
    '''
    nid = models.AutoField(primary_key=True)
    content = models.TextField()
    article = models.OneToOneField(to='Article', to_field='nid')


class ArticlUpDown(models.Model):
    '''
    点赞表
    '''
    nid = models.AutoField(primary_key=True)
    is_up = models.BooleanField(default=True)
    user = models.ForeignKey(to='UserInfo', to_field='nid', null=True)
    article = models.ForeignKey(to='Article', to_field='nid', null=True)

    class Meta:
        unique_together = (('user', 'article'),)


class Comment(models.Model):
    '''
    评论表
    '''
    nid = models.AutoField(primary_key=True)
    content = models.CharField(max_length=255)  # 评论内容
    parent_content = models.ForeignKey('self', null=True)  # 父评论
    crate_time = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(to='UserInfo', to_field='nid')
    article = models.ForeignKey(to='Article', to_field='nid')

    def __str__(self):
        return self.content

 

posted @ 2018-09-24 02:56  与鹿逐秋  阅读(274)  评论(0编辑  收藏  举报