Table Relationship Design publishing house table relationship
# Publisher Table class Publisher (models. Model): name = models. Charfield (max_length=32) addr = models. Charfield (max_length=128) phone = models. Integerfield () def __str__ (self): return self.name# Author table class Author (models. Model): name = models. Charfield (max_length=16) # author and details indicate a one-to-one association detail = models. Onetoonefield (to= "Authordetail") # The relationship between the author and the book is many-to-many books = models. Manytomanyfield (to= "book") def __str__ (self): return self.name# author Details table class Authordetail (models. Model): City = models. Charfield (max_length=32) email = models. Emailfield () class book (Models. Model): title = models. Charfield (max_length=16) Price = models. Decimalfield (Max_digits=5, decimal_places=2) # 5 digits, two decimal places 999.99 # Auto_now=true The time is automatically updated for each modification, and the first time the auto_now_add=true is created from Fill Time Publish_day = models. Datefield (auto_now_add=true) # Books and publishers are many-to-one, associated with publisher = models through foreign keys. ForeignKey (to= "Publisher") Memo = models. Charfield (max_length=128, null=true) def __str__ (self): return to self.Title
Blog system table Relationships
From django.contrib.auth.models import Abstractuserclass UserInfo (abstractuser): "" "User Information" "" Nid = Models. Autofield (primary_key=true) nickname = models. Charfield (verbose_name= ' nickname ', max_length=32) telephone = models. Charfield (max_length=11, Null=true, unique=true) avatar = models. Filefield (upload_to = ' avatars/', default= "/avatars/default.png") Create_time = models. Datetimefield (verbose_name= ' creation time ', auto_now_add=true) blog = models. Onetoonefield (to= ' blog ', to_field= ' nid ', null=true) def __str__ (self): return Self.usernameclass Blog (models. Model): "" "Blog Info" "" Nid = Models. Autofield (primary_key=true) title = models. Charfield (verbose_name= ' personal blog title ', max_length=64) site = models. Charfield (verbose_name= ' personal blog suffix ', max_length=32, unique=true) theme = models. Charfield (verbose_name= ' blog theme ', max_length=32) def __str__ (self): return Self.titleclass Category (models. Model): "" "Bo Master Personal article Classification table" "Nid = Models. Autofield (Primary_kEy=true) title = models. Charfield (verbose_name= ' category title ', max_length=32) blog = models. ForeignKey (verbose_name= ' belongs to blog ', to= ' blog ', to_field= ' nid ') def __str__ (self): return Self.titleclass Tag (models. Model): Nid = Models. Autofield (primary_key=true) title = models. Charfield (verbose_name= ' tag name ', max_length=32) blog = models. ForeignKey (verbose_name= ' belongs to blog ', to= ' blog ', to_field= ' nid ') def __str__ (self): return self.titleclass article (mod Els. Model): Nid = Models. Autofield (primary_key=true) title = models. Charfield (max_length=50, verbose_name= ' article title ') desc = models. Charfield (max_length=255, verbose_name= ' article description ') comment_count= models. Integerfield (default=0) Up_count = models. Integerfield (default=0) Down_count = models. Integerfield (default=0) Create_time = models. Datetimefield (verbose_name= ' creation time ') Homecategory = models. ForeignKey (to= ' Category ', to_field= ' nid ', null=true) #siteDetaiCategory = models. ForeignKey (to= ' sitecategory ', to_field= 'Nid ', null=true) user = models. ForeignKey (verbose_name= ' author ', to= ' UserInfo ', to_field= ' nid ') tags = models. Manytomanyfield (to= "tag", through= ' Article2tag ', through_fields= (' article ', ' tag '), Def __st R__ (self): return Self.titleclass Articledetail (models. Model): "" "Article Detail table" "Nid = Models. Autofield (primary_key=true) content = models. TextField () Article = models. Onetoonefield (to= ' article ', to_field= ' nid ') class Comment (models. Model): "" "Comment Table" "" Nid = Models. Autofield (primary_key=true) Article = models. ForeignKey (verbose_name= ' review article ', to= ' article ', to_field= ' nid ') user = models. ForeignKey (verbose_name= ' reviewer ', to= ' UserInfo ', to_field= ' nid ') content = models. Charfield (verbose_name= ' comment content ', max_length=255) Create_time = models. Datetimefield (verbose_name= ' creation time ', auto_now_add=true) parent_comment = models. ForeignKey ("Self", null=true) def __str__ (self): return Self.contentclass Articleupdown (models. Model): "" "click Like Table" "Nid = Models. Autofield (primary_key=true) user = models. ForeignKey (' UserInfo ', null=true) Article = models. ForeignKey ("article", Null=true) is_up=models. Booleanfield (default=true) class meta:unique_together = [(' article ', ' user '),]class article 2Tag (models. Model): Nid = Models. Autofield (primary_key=true) Article = models. ForeignKey (verbose_name= ' article ', to= "article", to_field= ' nid ') tag = models. ForeignKey (verbose_name= ' tag ', to= "tag", to_field= ' nid ') class meta:unique_together = [(' article ', ' Tag '),] def __str__ (self): v=self.article.title+ "----" +self.tag.title return V
Permissions system table Relationships
Class Menu (models. Model): Caption=models. Charfield (MAX_LENGTH=32) def __str__ (self): return Self.captionclass UserInfo (models. Model): Name=models. Charfield (max_length=32) pwd=models. Charfield (max_length=32,default=123) email=models. Emailfield () roles=models. Manytomanyfield (to= "role") def __str__ (self): return Self.nameclass Role (models. Model): Title=models. Charfield (max_length=32) permissions=models. Manytomanyfield (to= "Permission") def __str__ (self): return Self.titleclass Permission (models. Model): Url=models. Charfield (max_length=32) title=models. Charfield (max_length=32) permission_group=models. ForeignKey ("Permissiongroup", Default=1) code=models. Charfield (max_length=32,default= "") parent=models. ForeignKey ("Self", default=1,null=true,blank=true) def __str__ (self): return Self.titleclass permissiongroup (mode Ls. Model): Caption=models. Charfield (max_length=32) menu=models. ForeignKey ("menu", default=1) def __sTr__ (self): return self.caption
Database table Relationship Design reference