text: #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  from django.db import models
  from django.contrib.auth.models import User
  
  # Create your models here.
  
  
  class Category(models.Model):
      STATUS_NORMAL = 1
      STATUS_DELETE = 0
      STATUS_ITEMS = (
          (STATUS_NORMAL, '正常'),
          (STATUS_DELETE, '删除'),
      )
  
      name = models.CharField(verbose_name="名称", max_length=50)
      status = models.PositiveIntegerField(
          verbose_name="状态", default=STATUS_NORMAL, choices=STATUS_ITEMS)
      is_nav = models.BooleanField(verbose_name="是否为导航", default=False)
      # 在django2.0后，定义外键和一对一关系的时候需要加on_delete选项，
      # 此参数为了避免两个表里的数据不一致问题，不然会报错：
      # TypeError: __init__() missing 1 required positional argument: 'on_delete'
      owner = models.ForeignKey(User, verbose_name="作者",
                                on_delete=models.CASCADE)
      created_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
  
      class Meta:
          verbose_name = verbose_name_plural = '分类'
  
  
  class Tag(models.Model):
      STATUS_NORMAL = 1
      STATUS_DELETE = 0
      STATUS_ITEMS = (
          (STATUS_NORMAL, '正常'),
          (STATUS_DELETE, '删除'),
      )
  
      name = models.CharField(verbose_name="名称", max_length=14)
      status = models.PositiveIntegerField(
          verbose_name="状态", default=STATUS_NORMAL, choices=STATUS_ITEMS)
      owner = models.ForeignKey(User, verbose_name="作者",
                                on_delete=models.CASCADE)
      created_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
  
      class Meta:
          verbose_name = verbose_name_plural = '标签'
  
  
  class Post(models.Model):
      STATUS_NORMAL = 1
      STATUS_DELETE = 0
      STATUS_DRAFT = 2
      STATUS_ITEMS = (
          (STATUS_NORMAL, '正常'),
          (STATUS_DELETE, '删除'),
          (STATUS_DRAFT, '草稿'),
      )
  
      name = models.CharField(verbose_name="标题", max_length=255)
      status = models.PositiveIntegerField(
          verbose_name="状态", default=STATUS_NORMAL, choices=STATUS_ITEMS)
      desc = models.CharField(verbose_name="摘要", max_length=1024, blank=True)
      content = models.TextField(
          verbose_name="正文", help_text="正文必须为 MarkDown 样式")
      category = models.ForeignKey(Category, verbose_name="分类",
                                   on_delete=models.CASCADE)
      owner = models.ForeignKey(User, verbose_name="作者",
                                on_delete=models.CASCADE)
      tag = models.ManyToManyField(Tag, verbose_name="标签")
      created_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
  
      class Meta:
          verbose_name = verbose_name_plural = '文章'
          ordering = ['-id']  # 根据ID进行降序排列
  