class
Publisher(models.Model):
id
= models.AutoField(primary_key=
True)
name
= models.CharField(max_length=64,null=False,unique=
True)
def
__str__
(self):
return
"
publisher_name:{}
"
.format(self.name)
class
Book(models.Model):
id
= models.AutoField(primary_key=
True)
title
= models.CharField(max_length=128,null=
False)
publisher
= models.ForeignKey(to=Publisher,related_name=
"
books
"
)
def
__str__
(self):
return
"
book_title:{}
"
.format(self.title)
class
Author(models.Model):
id
= models.AutoField(primary_key=
True)
name
= models.CharField(max_length=16,null=
False)
book
= models.ManyToManyField(to=
"
Book
"
)
def
__str__
(self):
return
"
author_name:{}
"
.format(self.name)
进入的项目地址中,
输入python manage.py makemigrations
输入python manage.py migrate
我们就通过django生成了四张表:其中是以app为前缀的,我这里是person;存在关联的表之间,名字用_连接,并命名。
接下来我们填充一些数据,
在项目路径下输入python manage.py shell打开django终端:
我们举一个例子:
为方便起见,我们其他的数据在navicat中手动填充。最后效果如下(person_publisher,person_author,person_book,person_author_book):