Django 개인 블로그 구축 자습서---models 데이터베이스 모델

4250 단어 Django
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)

좋은 웹페이지 즐겨찾기