标签:作者 integer any sel 名称 cad sed hone creat
1 from django.db import models 2 3 # Create your models here. 4 5 6 from django.contrib.auth.models import AbstractUser 7 8 9 class UserInfo(AbstractUser): 10 """ 11 用户信息 12 """ 13 nid = models.AutoField(primary_key=True) 14 telephone = models.CharField(max_length=11, null=True, unique=True) 15 avatar = models.FileField(upload_to=‘avatars/‘, default="avatars/default.png") 16 create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) 17 18 blog = models.OneToOneField(to=‘Blog‘, to_field=‘nid‘, null=True, on_delete=models.CASCADE) 19 20 def __str__(self): 21 return self.username 22 23 24 class Blog(models.Model): 25 """ 26 博客信息 27 """ 28 nid = models.AutoField(primary_key=True) 29 title = models.CharField(verbose_name=‘个人博客标题‘, max_length=64) 30 site_name = models.CharField(verbose_name=‘站点名称‘, max_length=64) 31 theme = models.CharField(verbose_name=‘博客主题‘, max_length=32) 32 33 def __str__(self): 34 return self.title 35 36 37 class Category(models.Model): 38 """ 39 博主个人文章分类表 40 """ 41 nid = models.AutoField(primary_key=True) 42 title = models.CharField(verbose_name=‘分类标题‘, max_length=32) 43 blog = models.ForeignKey(verbose_name=‘所属博客‘, to=‘Blog‘, to_field=‘nid‘, on_delete=models.CASCADE) 44 45 def __str__(self): 46 return self.title 47 48 49 class Tag(models.Model): 50 nid = models.AutoField(primary_key=True) 51 title = models.CharField(verbose_name=‘标签名称‘, max_length=32) 52 blog = models.ForeignKey(verbose_name=‘所属博客‘, to=‘Blog‘, to_field=‘nid‘, on_delete=models.CASCADE) 53 54 def __str__(self): 55 return self.title 56 57 58 class Article(models.Model): 59 nid = models.AutoField(primary_key=True) 60 title = models.CharField(max_length=50, verbose_name=‘文章标题‘) 61 desc = models.CharField(max_length=255, verbose_name=‘文章描述‘) 62 create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) 63 content = models.TextField() 64 65 comment_count = models.IntegerField(default=0) 66 up_count = models.IntegerField(default=0) 67 down_count = models.IntegerField(default=0) 68 69 user = models.ForeignKey(verbose_name=‘作者‘, to=‘UserInfo‘, to_field=‘nid‘, on_delete=models.CASCADE) 70 category = models.ForeignKey(to=‘Category‘, to_field=‘nid‘, null=True, on_delete=models.CASCADE) 71 tags = models.ManyToManyField( 72 to="Tag", 73 through=‘Article2Tag‘, 74 through_fields=(‘article‘, ‘tag‘), 75 ) 76 77 def __str__(self): 78 return self.title 79 80 81 class Article2Tag(models.Model): 82 nid = models.AutoField(primary_key=True) 83 article = models.ForeignKey(verbose_name=‘文章‘, to="Article", to_field=‘nid‘, on_delete=models.CASCADE) 84 tag = models.ForeignKey(verbose_name=‘标签‘, to="Tag", to_field=‘nid‘, on_delete=models.CASCADE) 85 86 class Meta: 87 unique_together = [ 88 (‘article‘, ‘tag‘), 89 ] 90 91 def __str__(self): 92 v = self.article.title + "---" + self.tag.title 93 return v 94 95 96 class ArticleUpDown(models.Model): 97 """ 98 点赞表 99 """ 100 101 nid = models.AutoField(primary_key=True) 102 user = models.ForeignKey(‘UserInfo‘, null=True, on_delete=models.CASCADE) 103 article = models.ForeignKey("Article", null=True, on_delete=models.CASCADE) 104 is_up = models.BooleanField(default=True) 105 106 class Meta: 107 unique_together = [ 108 (‘article‘, ‘user‘), 109 ] 110 111 112 class Comment(models.Model): 113 """ 114 115 评论表 116 117 """ 118 nid = models.AutoField(primary_key=True) 119 article = models.ForeignKey(verbose_name=‘评论文章‘, to=‘Article‘, to_field=‘nid‘, on_delete=models.CASCADE) 120 user = models.ForeignKey(verbose_name=‘评论者‘, to=‘UserInfo‘, to_field=‘nid‘, on_delete=models.CASCADE) 121 content = models.CharField(verbose_name=‘评论内容‘, max_length=255) 122 create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) 123 parent_comment = models.ForeignKey(‘self‘, null=True, on_delete=models.CASCADE) 124 125 def __str__(self): 126 return self.content
上面是models的写法。
要注意的是,因为我们使用的是自己的user_Info表所以,在settings中需要告诉django一下:
AUTH_USER_MODEL = "blog.UserInfo"
下图是关系表:
标签:作者 integer any sel 名称 cad sed hone creat
原文地址:https://www.cnblogs.com/zhoulixiansen/p/9315130.html