一、创建模型类
class Publish(models.Model):
name = models.CharField(max_length=64)
city = models.CharField(max_length=63,null=True)
def str(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=30)
sex = models.CharField(max_length=20)
def str(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=64)
price = models.IntegerField()
color = models.CharField(max_length=64)
page_num = models.IntegerField(null=True)
publisher = models.ForeignKey("Publish",on_delete=models.CASCADE,null=True) #一对多的关系。2.0django中,当有主外键和其他对应关系时,需要设置。
author = models.ManyToManyField("Author")
def str(self):
return self.title
二、生成数据表
将类转换成数据表结构
python manage.py makemigrations
根据上一句代码生成数据表
python manage.py migrate