django orm support in helping us create the correspondence between the table and the table. One to one, one to many and many to many.
If we want to create
- Book Table
- Press table
- On the table
- Author Details Table
Book publishers table is a table with many relationship. (Assuming that only a book published by a publishing house)
Many to many relationship between the table and the author table books.
It is one to one relationship between the author and the author details table table.
models.py
-------------------------
class Book(models.Model):
title = models.CharField(max_length=254)
price = models.DecimalField(max_digits=8, decimal_places=2)
# 与出版社的外键对应关系 一对多
publish = models.ForeignKey(to='Publish')
# 与作者的对应关系 多对多---会自动生成 关联表
author = models.ManyToManyField(to='Author')
class Author(models.Model):
name = models.CharField(max_length=254)
phone = models.BigIntegerField()
# 与作者详情表的对应关系 一对多
author_detail = models.OneToOneField(to='AuthorDetail')
class AuthorDetail(models.Model):
age = models.IntegerField()
addr = models.CharField(max_length=254)
class Publish(models.Model):
name = models.CharField(max_length=254)
addr = models.CharField(max_length=254)