• 边学边练之博客园----设计表


     1 from django.db import models
     2 
     3 # Create your models here.
     4 from django.db import models
     5 from django.contrib.auth.models import AbstractUser
     6 
     7 
     8 # Create your models here.
     9 
    10 class UserInfo(AbstractUser):
    11     nid = models.AutoField(primary_key=True)
    12     tel = models.CharField(max_length=11, null=True, unique=True)
    13     avatar = models.FileField(upload_to='avatars/', default='avatars/default.png')
    14     create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    15 
    16     blog = models.OneToOneField(to='Blog', to_field='nid', null=True, on_delete=models.CASCADE)
    17 
    18     def __str__(self):
    19         return self.username
    20 
    21 
    22 class Blog(models.Model):
    23     nid = models.AutoField(primary_key=True)
    24     title = models.CharField(verbose_name='个人博客标题', max_length=64)
    25     site_name = models.CharField(verbose_name='个人站点名称', max_length=64)
    26     theme = models.CharField(verbose_name='主题', max_length=32)
    27 
    28 
    29 class Category(models.Model):
    30     nid = models.AutoField(primary_key=True)
    31     title = models.CharField(max_length=32)
    32 
    33     blog = models.ForeignKey(to='Blog', to_field='nid', on_delete=models.CASCADE)
    34 
    35     def __str__(self):
    36         return self.title
    37 
    38 
    39 class Tag(models.Model):
    40     nid = models.AutoField(primary_key=True)
    41     title = models.CharField(max_length=32)
    42 
    43     blog = models.ForeignKey(to='Blog', to_field='nid', on_delete=models.CASCADE)
    44 
    45     def __str__(self):
    46         return self.title
    47 
    48 
    49 class Article(models.Model):
    50     nid = models.AutoField(primary_key=True)
    51     title = models.CharField(max_length=64)
    52     desc = models.CharField(max_length=255)
    53     create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    54 
    55     content = models.TextField()
    56 
    57     comment_count = models.IntegerField(default=0)            # 直接在这里加上这几项,就不需要去相关表里查找了
    58     up_count = models.IntegerField(default=0)
    59     down_count = models.IntegerField(default=0)
    60 
    61     user = models.ForeignKey(to='UserInfo',to_field='nid',on_delete=models.CASCADE)
    62     category = models.ForeignKey(to='Category',to_field='nid',null=True,on_delete=models.CASCADE)
    63     tags =  models.ManyToManyField(to='Tag',through='Article2Tag',through_fields=('article','tag'))
    64 
    65     def __str__(self):
    66         return self.title
    67 
    68 class Article2Tag(models.Model):
    69     nid = models.AutoField(primary_key=True)
    70     article = models.ForeignKey(to='Article',to_field='nid',on_delete=models.CASCADE)
    71     tag = models.ForeignKey(to='Tag',to_field='nid',on_delete=models.CASCADE)
    72 
    73     class Meta:
    74         unique_together=[('article','tag')]  # 联合唯一
    75 
    76     def __str__(self):
    77         v = self.article.title + "---" + self.tag.title
    78         return v
    79 
    80 class ArticleUpDown(models.Model):
    81     nid = models.AutoField(primary_key=True)
    82     article = models.ForeignKey(to='Article',to_field='nid',null=True,on_delete=models.CASCADE)
    83     user = models.ForeignKey(to='UserInfo',to_field='nid',null=True,on_delete=models.CASCADE)
    84     is_up = models.BooleanField(default=True)
    85 
    86     class Meta:
    87         unique_together=[('article','user')]
    88 
    89 class Comment(models.Model):
    90     nid=models.AutoField(primary_key=True)
    91     article = models.ForeignKey(to='Article',to_field='nid',on_delete=models.CASCADE)
    92     user = models.ForeignKey(to='UserInfo',to_field='nid',on_delete=models.CASCADE)
    93     content = models.CharField(max_length=255)
    94     create_time = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)
    95     parent_comment = models.ForeignKey('self',null=True,on_delete=models.CASCADE)
    96 
    97     def __str__(self):
    98         return self.content
  • 相关阅读:
    swift 初见-4运算符与字符串操作
    IOS中数据持久化1-CoreData
    swift 初见-3
    swift 初见-2
    系统硬件1-短信,打电话
    swift 初见-1
    socket理解流程图
    文件操作方法fscanf
    Prim模板
    树剖求LCA模板
  • 原文地址:https://www.cnblogs.com/yuliangkaiyue/p/9951126.html
Copyright © 2020-2023  润新知