Python Learning---drawer frame analysis [Database design analysis]180313

Source: Internet
Author: User
Tags sendmsg

Basic:

models.py

################################### #以下都是抽屉的代码 ################################### #from django.db Import models# Records the number of user registrations class sendmsg (models. Model): Nid = Models. Autofield (primary_key=true) # Custom ID named Nid, the default ID name is ID "fixed format" email = models. Charfield (max_length=32, unique=true) code = models. Charfield (max_length=6) CTime = models. Datetimefield () times = models. Integerfield (default=0) # News type class Newstype (models. Model): Caption = models. Charfield (max_length=16) # Drawer User table Design class UserInfo (models. Model): Nid = Models. Autofield (primary_key=true) Username = models. Charfield (max_length=32, unique=true) password = models. Charfield (max_length=32) email = models. Charfield (max_length=32, unique=true) CTime = models. Datetimefield () # News form models. Model): # You can create your own id,autofield and Primary_key = True "Default Id=id" title = models. Charfield (max_length=64) # Title Summary = models. Charfield (max_length=128, null=true) # [content, can be empty introduction] URL = models. Urlfield (null=true) CTime = Models. Datetimefield (auto_now_add=true) # The following 2 user effects are the same, except that the notation is different # user = models. ForeignKey (' UserInfo ', on_delete=true, to_field= ' nid ') # is associated with the nid of UserInfo user = models. ForeignKey (to= "UserInfo", to_field= ' nid ', related_name= ' n ', on_delete=true) # is associated with the UserInfo table NT = models. ForeignKey (to= ' Newstype ', to_field= ' id ', related_name= ' tn ', on_delete=true) # association Newstype table, associating default ID # referencing the following line of code, you do not need the following Favor table, 2 One effect "Here is the point like table implementation, so commented" # favor = models. Manytomanyfield (to= ' UserInfo ') # The default association is the primary key Favor_count = models. Integerfield (default=0) # add 1 for each time you like it, adding a data Comment_count = models to the top table. Integerfield (default=0) # adds 1 per review and adds a data to the comment table # like the table Class Favor (models. Model): User = models. ForeignKey (to= ' UserInfo ', to_field= ' nid ', on_delete=true) news = models. ForeignKey (to= ' News ', to_field= ' id ', on_delete=true) # comment Form class Comment (models. Model): News = models. ForeignKey (to= ' News ', to_field= ' id ', on_delete=true) user = models. ForeignKey (to= ' UserInfo ', to_field= ' nid ', On_delete=true) content = models. Charfield (max_length=150) CTime = models. Datetimefield (auto_now_add=true) device = models. Charfield (Max_length=16, null=true) # Top stepping table: Record which news you have over that user, and which news of which users have stepped on

High-level:

Here we use Many2many to achieve a many-to-many relationship between news and Useinfo

We are in news is used to favor_count and comment_count to achieve the page display of the number of likes and comments on the number of statistics, the subsequent pages have data over time, we give Favor_count and Comment_count plus 1, and insert the data in the comment and favor tables. [Although it consumes the hard drive, but improves the speed]

models.py

################################### #以下都是抽屉的代码 ################################### #from django.db Import models# Records the number of user registrations class sendmsg (models. Model): Nid = Models. Autofield (primary_key=true) # Custom ID named Nid, the default ID name is ID "fixed format" email = models. Charfield (max_length=32, unique=true) code = models. Charfield (max_length=6) CTime = models. Datetimefield () times = models. Integerfield (default=0) # News type class Newstype (models. Model): Caption = models. Charfield (max_length=16) # Drawer User table Design class UserInfo (models. Model): Nid = Models. Autofield (primary_key=true) Username = models. Charfield (max_length=32, unique=true) password = models. Charfield (max_length=32) email = models. Charfield (max_length=32, unique=true) CTime = models. Datetimefield () # News form models. Model): # You can create your own id,autofield and Primary_key = True "Default Id=id" title = models. Charfield (max_length=64) # Title Summary = models. Charfield (max_length=128, null=true) # [content, can be empty introduction] URL = models. Urlfield (null=true) CTime = Models. Datetimefield (auto_now_add=true) # The following 2 user effects are the same, except that the notation is different # user = models. ForeignKey (' UserInfo ', on_delete=true, to_field= ' nid ') # is associated with the nid of UserInfo user = models. ForeignKey (to= "UserInfo", to_field= ' nid ', related_name= ' n ', on_delete=true) # is associated with the UserInfo table NT = models. ForeignKey (to= ' Newstype ', to_field= ' id ', related_name= ' tn ', on_delete=true) # association Newstype table, associating default ID # referencing the following line of code, you do not need the following Favor table, 2 One effect "do not use the point like table favor" favor = models. Manytomanyfield (to= ' UserInfo ') # The default association is the primary key Favor_count = models. Integerfield (default=0) # add 1 for each time you like it, adding a data Comment_count = models to the top table. Integerfield (default=0) # added 1 per review and added a data # like table to the comment table "Here is the Manytomany for UserInfo and news alone, so it's commented" # Class Favor (models. Model): # user = models. ForeignKey (to= ' UserInfo ', to_field= ' nid ', on_delete=true) # news = models. ForeignKey (to= ' News ', to_field= ' id ', on_delete=true) # comment Form class Comment (models. Model): News = models. ForeignKey (to= ' News ', to_field= ' id ', on_delete=true) user = models. ForeIgnkey (to= ' UserInfo ', to_field= ' nid ', on_delete=true) content = models. Charfield (max_length=150) CTime = models. Datetimefield (auto_now_add=true) device = models. Charfield (Max_length=16, null=true) # Top stepping table: Record which news you have over that user, and which news of which users have stepped on

Ultimate Edition: Boost speed, add features

models.py

################################### #以下都是抽屉的代码 ################################### #from django.db Import models# Records the number of user registrations class sendmsg (models. Model): Nid = Models. Autofield (primary_key=true) # Custom ID named Nid, the default ID name is ID "fixed format" email = models. Charfield (max_length=32, unique=true) code = models. Charfield (max_length=6) CTime = models. Datetimefield () times = models. Integerfield (default=0) # News type class Newstype (models. Model): Caption = models. Charfield (max_length=16) # Drawer User table Design class UserInfo (models. Model): Nid = Models. Autofield (primary_key=true) Username = models. Charfield (max_length=32, unique=true) password = models. Charfield (max_length=32) email = models. Charfield (max_length=32, unique=true) CTime = models. Datetimefield () # News form models. Model): # You can create your own id,autofield and Primary_key = True "Default Id=id" title = models. Charfield (max_length=64) # Title Summary = models. Charfield (max_length=128, null=true) # [content, can be empty introduction] URL = models. Urlfield (null=true) CTime = Models. Datetimefield (auto_now_add=true) # The following 2 user effects are the same, except that the notation is different # user = models. ForeignKey (' UserInfo ', on_delete=true, to_field= ' nid ') # is associated with the nid of UserInfo user = models. ForeignKey (to= "UserInfo", to_field= ' nid ', related_name= ' n ', on_delete=true) # is associated with the UserInfo table # NT = models. ForeignKey (to= ' Newstype ', to_field= ' id ', related_name= ' tn ', on_delete=true) # association Newstype table, associated with default ID # because the content is fixed, so here is the column directly The form of the table is news_type_choices = [(1, ' 42 zone '), (2, ' Satin '), (3, ' picture '), (4, ' kicked 1024 '), (5, ' you ask I answer '),] # Djangoadmin inside will render as a drop-down box, here we need to write down the dropdown box NT = models. Integerfield (choices=news_type_choices) # Reference the following line of code, you do not need the following favor table, 2 one effect favor = models. Manytomanyfield (to= ' UserInfo ') # The default association is the primary key Favor_count = models. Integerfield (default=0) # add 1 for each time you like it, adding a data Comment_count = models to the top table. Integerfield (default=0) # 1 per review, add a data to the comment table # like a table "here we use Many2many to achieve a many-to-many relationship, this table is temporarily not" # class Favor (models. Model): # user = models. ForeignKey (to= ' UserInfo ', To_field= ' nid ', on_delete=true) # news = models. ForeignKey (to= ' News ', to_field= ' id ', on_delete=true) # comment Form class Comment (models. Model): News = models. ForeignKey (to= ' News ', to_field= ' id ', on_delete=true) user = models. ForeignKey (to= ' UserInfo ', to_field= ' nid ', on_delete=true) content = models. Charfield (max_length=150) CTime = models. Datetimefield (auto_now_add=true) device = models. Charfield (Max_length=16, null=true) # This requires the parent comment to be a number only and the self-increment ID # Inside the comment can also implement reverse lookup "This can be considered as 2 comment tables, but you need to add releat_name, no It is easy to confuse "parent_comment = models." ForeignKey (to= "Comment", Null=true, On_delete=true, related_name= ' pc ') # Top stepping table: Record which news you have over that user, which news you've stepped on.

Python Learning---drawer framework analysis [Database design analysis]180313

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.