Django个人博客搭建教程---models数据库模型

from django.db import models
from mdeditor.fields import MDTextField
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.conf import settings
from datetime import date

'''class UserInfo(AbstractUser):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)'''


class Category(models.Model):
    """
        Django 要求模型必须继承 models.Model 类。
        Category 只需要一个简单的分类名 name 就可以了。
        CharField 指定了分类名 name 的数据类型,CharField 是字符型,
        CharField 的 max_length 参数指定其最大长度,超过这个长度的分类名就不能被存入数据库。
        当然 Django 还为我们提供了多种其它的数据类型,如日期时间类型 DateTimeField、整数类型 IntegerField 等等。
        Django 内置的全部类型可查看文档:
        https://docs.djangoproject.com/en/1.10/ref/models/fields/#field-types
        """
    name = models.CharField(max_length=100)

    def catcount(self):
        return Articles.objects.filter(category__name__exact=self.name).filter(status='有效').count()

    def __str__(self):
        return self.name


class Tag(models.Model):
    """
        标签 Tag 也比较简单,和 Category 一样。
        再次强调一定要继承 models.Model 类!
        """
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


# Create your models here.


class Articles(models.Model):
    id = models.AutoField(primary_key=True)  # id
    title = models.CharField(max_length=150)  # 博客标题
    body = models.TextField()  # 博客正文
    timestamp = models.DateTimeField()  # 创建时间
    authorname = models.ForeignKey('JiaBlog.BlogUser', on_delete=models.CASCADE)  # 作者姓名
    views = models.PositiveIntegerField(default=0)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, primary_key=False)
    tags = models.ManyToManyField(Tag, blank=True, null=True)
    greats = models.PositiveIntegerField(default=0)
    comments = models.IntegerField(default=0)
    status = models.CharField(max_length=20, default="DEL")
    brief = models.CharField(max_length=200, blank=True, null=True)
    pic = models.ImageField(upload_to='jiablogimages')
    bodypic = models.ImageField(upload_to='jiablogimages', blank=True, null=True)
    istop = models.CharField(max_length=5, default='', null=True, blank=True)
    articlebodybrief = models.TextField(blank=True, null=True)

    # 访问量

    def increase_views(self):
        self.views += 1
        self.save(update_fields=['views'])
        return self.views

    @property
    def all_comments(self):
        return self.comment_set.all()

    def article_greats(self):
        return self.greats


# Create your models here.


class Comment(models.Model):
    name = models.ForeignKey('JiaBlog.BlogUser', max_length=100, on_delete=models.CASCADE)
    email = models.EmailField(max_length=255)
    # email = name.email
    text = models.TextField()
    created_time = models.DateTimeField(auto_now_add=True)
    post = models.ForeignKey('JiaBlog.Articles', on_delete=models.CASCADE)
    parentcomment = models.ForeignKey('JiaBlog.Comment', on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.text[:20]

    def ifnochild(self):
        '''if Comment.objects.filter(comment__comment__post_id=self.post_id).count() == 0:
            print('1')
            return True
            else:
            return False'''
        if self.comment_set.all().count() != 0:
            return 1
        else:
            return 0


class Message(models.Model):
    username = models.CharField(max_length=256)
    title = models.CharField(max_length=512)
    content = models.TextField(max_length=256)
    email = models.EmailField()
    publish = models.DateTimeField(auto_now_add=True)
    phone = models.CharField(max_length=11, blank=True, default="", null=True)
    messpic = models.CharField(max_length=50, null=True)

    # 为了显示
    def __str__(self):
        tpl = ''
        return tpl.format(username=self.username, title=self.title, content=self.content, publish=self.publish)

 

你可能感兴趣的:(Django)