Django operating table - relationships between tables (based on MySQL)

Django operating table - relationships between tables (based on MySQL)

Relationships between data tables are: one to many, and many-to-one, ready to establish relationships between tables in Django project

Data Sheet: book, book_detail, author, publish

Data relationships:

Many to many: book and author

Many: book and publish

One: book and book_detail

All foreign key recommendation based on query frequency goes higher in the table

# 此处是models.py 文件
from django.db import models


# 1.创建基本表对应的类
class Book(models.Model):
    b_id = models.AutoField(primary_key=True)
    book_name = models.CharField(max_length=32)
    # DecimalFeild表示该字段是小数,max_digits表示数字位数,decimal_places表示保留小数位数,这个表示price为小数字段,总共8位,小数位占2位
    price = models.DecimalField(max_digits=8, decimal_places=2)
    is_delete = models.IntegerField(default=0)
    
    '''2.创建一对多的表关系:models.ForeignKey()
    book表和publish表是一对多的外键关键,创建外键关系
    默认关联字段是publish表的主键字段
    to='Publish' 也可以写成to=Publish,即to=表的变量名,但要保证,该变量名在当前类的上方,不推荐这种方式
    '''
    publish = models.ForeignKey(to='Publish')
    
    '''3.创建多对多的表关系:models.ManyToManyField()
    book表和author表是多对多的外键关系,创建外键关系
    注意:authors字段是虚拟字段,Django在创建同步数据库的过程中,不会创建该字段,而是创建book和auth这两张表之间的关系需要创建第三张表
    '''
    authors = models.ManyToManyField(to='Author')
    
    '''4.创建一对一的表关系:models.OneToOneField()
    '''
    book_detail = models.OneToOneField(to='BookDetail')
    
    
class BookDetail(models.Model):
    d_id = models.AutoField(primary_key=True)
    content = models.CharField(max_length=128)
    sale_num = models.IntegerField()
    star = models.IntegerField()
    
    
class Publish(models.Model):
    p_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64)
    addr = models.CharField(max_length=64)
    
    
class Author(models.Model):
    a_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    phone = models.BigIntegerField()

to sum up:

Create a foreign key, divided into three relationships

Note: After the class corresponding to the table and the corresponding relationship is created, we must commit command! ! !

1. many: variable name = models.ForeignKey (to = 'foreign key table name')

Django automatically informed MySQL, the foreign key field requires increased idsuffix , thus defining a variable, the variable is not required to specify its own primary key the foreign key table , has itself increased Django

2-many: variable name = models.ManyToManyField (to = 'foreign key table name')

  • Django automatically informed MySQL, the foreign key field requires increased idsuffix , thus defining a variable, the variable is not required to specify its own primary key the foreign key table , has itself increased Django
  • Django according ManyToManyField, told MySQL, in addition to creating two base tables, but also need to create additional third table to store the relationship between the two base tables , table name to 主表名_外键表名name , inside the field into two groups of tables primary key

3-one: variable name = models.OneToOneField (to = 'foreign key table name')

Django automatically informed MySQL, the foreign key field requires increased idsuffix , thus defining a variable, the variable is not required to specify its own primary key the foreign key table , has itself increased Django

Guess you like

Origin www.cnblogs.com/xiaodan1040/p/12158000.html