blog建表操作
阿新 • • 發佈:2017-12-04
時間 郵箱 box max 關聯 django nco length none
表思維導圖:
數據庫:表
from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserInfo(AbstractUser): #settings :AUTH_USER_MODEL ="項目名稱.UserInfo"
‘‘‘用戶信息表‘‘‘
nid = models.BigAutoField(primary_key=True)
nickname =models.CharField(max_length=32,verbose_name="昵稱",unique=True)
tel = models.IntegerField(verbose_name="電話",unique=True,null=True,blank=True)
email = models.CharField(max_length=64,verbose_name="郵箱")
avatar = models.FileField(verbose_name="頭像",upload_to="avatar",default="/avatar/default.png")
create_time = models.DateTimeField(verbose_name="創建時間",auto_now_add=True)
class Meta:
verbose_name_plural = "用戶信息表"
def __str__(self):
return self.username
class Article(models.Model):
‘‘‘
文章表
‘‘‘
title = models.CharField(max_length=64,verbose_name="文章標題")
summary = models.CharField(max_length=244, verbose_name="文章概要")
create_time = models.DateTimeField(verbose_name="創建時間",auto_now_add=True)
update_time = models.DateTimeField(verbose_name="修改時間",auto_now=True)
poll_count = models.IntegerField(verbose_name="點贊數",default=0)
comment_count = models.IntegerField(verbose_name="評論數",default=0)
read_count = models.IntegerField(verbose_name="閱讀數",default=0)
# is_essence = models.BooleanField(verbose_name="是否精華",default=0)
# is_top = models.BooleanField(verbose_name="是否置頂",default=0)
user = models.ForeignKey(to="UserInfo",verbose_name="所屬作者",null=True,blank=True)
classify = models.ForeignKey(to="Classfication",verbose_name="所屬類別",null=True,blank=True)
tags = models.ManyToManyField(to="Tag",through="Article2tag",through_fields=(‘article‘, ‘tag‘),verbose_name="所屬標簽")
type_choices = [
(1,"編程語言"),
(2,"軟件設計"),
(3,"前端系列"),
(4,"數據庫"),
(5,"操作系統")
]
artcle_type_id = models.IntegerField(choices=type_choices,default=None)
class Meta:
verbose_name_plural = "文章表"
def __str__(self):
return self.title
class Article_detail(models.Model):
‘‘‘文章細節表‘‘‘
article = models.OneToOneField(to="Article",verbose_name="所屬文章")
content =models.TextField(max_length=400,verbose_name="文章內容")
class Meta:
verbose_name_plural = "文章細節表"
class Tag(models.Model):
‘‘‘標簽表‘‘‘
name = models.CharField(max_length=32,verbose_name="標簽名")
blog = models.ForeignKey(to="Blog",verbose_name="所屬博客")
class Meta:
verbose_name_plural = "標簽表"
class Article2tag(models.Model):
article = models.ForeignKey(verbose_name="文章",to="Article")
tag = models.ForeignKey(verbose_name="標簽",to="Tag")
class Meta:
‘‘‘聯合唯一‘‘‘
unique_together = [
("article","tag")
]
class Comment(models.Model):
‘‘‘評論表‘‘‘
time = models.DateTimeField(verbose_name="評論時間",auto_now_add=True)
content = models.CharField(max_length=265,verbose_name="評論內容")
up_count = models.IntegerField(default=0)
user = models.ForeignKey(to="UserInfo",verbose_name="評論人",null=True,blank=True)
article = models.ForeignKey(to="Article",verbose_name="評論文章",null=True,blank=True)
farther_comment = models.ForeignKey(to="Comment",verbose_name="父級評論",null=True,blank=True)
# farther_comment = models.ForeignKey("self",verbose_name="父級評論",null=True,blank=True)
class Meta:
verbose_name_plural = "評論表"
class Article_poll(models.Model):
‘‘‘文章點贊表‘‘‘
time = models.DateTimeField(verbose_name="點贊時間",auto_now_add=True)
article = models.ForeignKey(to="Article",verbose_name="點贊文章",null=True,blank=True) #一個文章可以有多個贊
user = models.ForeignKey(to="UserInfo",verbose_name="點贊人",null=True,blank=True)
# is_positive = models.BooleanField(default=1,verbose_name="點贊或踩")
class Meta:
‘‘‘聯合唯一‘‘‘
unique_together = ("user", "article",)
verbose_name_plural = "文章點贊表"
class Comment_poll(models.Model):
‘‘‘評論點贊表‘‘‘
time=models.DateTimeField(verbose_name="點贊時間",auto_now_add=True)
# is_positive = models.BooleanField(verbose_name="點贊或踩",default=1)
user = models.ForeignKey(to="UserInfo",verbose_name="點贊用戶",null=True,blank=True)
comment = models.ForeignKey(to="Comment",verbose_name="點贊所屬評論",null=True,blank=True) #一個評論可以有多個贊
class Meta:
‘‘‘聯合唯一‘‘‘
unique_together = ("user","comment",)
verbose_name_plural = "評論點贊表"
class Blog(models.Model):
‘‘‘個人站點表‘‘‘
title = models.CharField(max_length=32,verbose_name="個人博客標題")
url = models.CharField(max_length=64,verbose_name="路徑",unique=True)
theme = models.CharField(max_length=32,verbose_name="博客主題")
user = models.OneToOneField(to="UserInfo", verbose_name="所屬用戶")
class Meta:
‘‘‘通過admin錄入數據的時候個中文顯示‘‘‘
verbose_name_plural = "個人站點表"
def __str__(self):
return self.title
class Classfication(models.Model):
‘‘‘博主個人文章分類表‘‘‘
title = models.CharField(max_length=32, verbose_name="分類標題")
blog = models.ForeignKey(to="Blog",verbose_name="所屬博客")
class Meta:
verbose_name_plural = "分類表"
註釋:
十張表:
用戶表(userinfo):
nickname(用戶昵稱)
telephone(手機號)
avatar(頭像)
create_time(創建時間)
站點表(Blog):
title(博客標題)
url(路徑)
theme(博客主題)
user和用戶表關系(one2one)
文章表(Article):
title(標題)
desc(內容)
read_count(文章閱讀量)
comment_count(文章評論量)
up_count(文章置頂)
down_count(文章置末尾)
create_time(文章創建時間)
category(類型)和分類表(fk)
user(所屬用戶)和用戶表(fk)
tags(標簽)和標簽表(m2m)
文章詳情表(ArticleDetail):
content(文章類容)
article(所屬文章)和文章表示(one2one)
文章分類表(Category): title(文章標題) blog(fk)
評論表(Comment):
content(評論語)
create_time(創建時間)
up_count(評論量)
user(評論者)和用戶(fk)
article(評論文章)和文章表(fk)
parent_comment(父級評論)自關聯
評論點贊表(CommentUp):
user(點贊的用戶)和用戶表(fk)
comment(點贊評論)和評論表(fk)
文章點贊表(ArticleUp):
user(點贊的用戶)和用戶表(fk)
comment(被點贊文章)和文章表(fk)
標簽表(Tag):
title(標簽名稱)
bolg(所屬博客)和博客表(fk)
文章和標簽表(Article2Tag):
article(文章)和文章表(fk)
tag(標簽)和標簽表(fk)
blog建表操作