- class Book(models.Model):
- title = models.CharField(max_length=64)
- """
- max_digits=None,:总位数
- decimal_places=None:小数位数
- """
- # price decimal(8,2)
- price = models.DecimalField(max_digits=8, decimal_places=2)
- # publish_id = models.ForeignKey(to='Publish', to_field='id')
- """对于外键字段关系,会自动帮我们拼接_id"""
- publish = models.ForeignKey(to='Publish')
- """authors它是一个虚拟字段,它不会实际在表中创建出来这个字段,这一句可以自动帮助我们创建出来第三张表"""
- authors = models.ManyToManyField(to='Author')
- """出版社表"""
- class Publish(models.Model):
- name = models.CharField(max_length=64)
- addr = models.CharField(max_length=64)
-
-
- """作者表"""
- class Author(models.Model):
- name = models.CharField(max_length=64)
- author_detail = models.OneToOneField(to='AuthorDetail')
-
- """作者详情表"""
- class AuthorDetail(models.Model):
- phone = models.CharField(max_length=64)
- email = models.CharField(max_length=64)
上面的代码是Django1.x版本的写法。Django2.x的版本有些许不同,需要在添加外键的时候手动加上级联删除(on_delete=models.CASCADE)
Django的请求生命周期流程图可以帮助我妈们熟练django的一个完整执行流程