邊學邊練之部落格園----設計表
阿新 • • 發佈:2018-11-13
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