from django.db import models # Create your models here. # 书 class Book(models.Model): title = models.CharField(max_length=32) publish_date = models.DateField(auto_now_add=True) price = models.DecimalField(max_digits=5, decimal_places=2) memo = models.TextField(null=True) # 创建外键,关联publish publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE, related_name='books') # 创建多对多关联author author = models.ManyToManyField(to="Author") def __str__(self): return self.title # 出版社 class Publisher(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32) def __str__(self): return self.name # 作者 class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() phone = models.CharField(max_length=11) detail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE) def __str__(self): return self.name # 作者详情 class AuthorDetail(models.Model): addr = models.CharField(max_length=64) email = models.EmailField()
表结构:
练习:
import os if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite05.settings") import django django.setup() # 在设置好环境变量以及导入Django项目之后才能找到Django项目下的APP # 所以必须要在这里导入 from app import models # 查找某个出版社关联的所有的书籍---反向查找 # ret = models.Publisher.objects.get(id='2').book_set.all() # 在Book中添加反向操作related_name之后 # ret = models.Publisher.objects.get(id='2').books.all() # 查找书名为七侠五义的出版社的名字 # Book类和Publisher定义的时候调用了__str__方法 # 实例化出类的时候自动打印出相应的名字 # ret = models.Book.objects.get(title='七侠五义').publisher # 查找所有书名里有传奇的书 # ret = models.Book.objects.filter(title__contains='传奇') # 查找出版日期为2017年的书籍 # ret = models.Book.objects.filter(publish_date__year=2017) # 查找出版日期是2018年的书名 # ret = models.Book.objects.filter(publish_date__year=2018).values('title') # 查找价格大于100元的书籍 # ret = models.Book.objects.filter(price__gt=100).values("title", "price") # 查找某字段为空的 # ret = models.Book.objects.filter(memo__isnull=True) # 查找在北京的出版社 # ret = models.Publisher.objects.filter(city='北京') # 查找名字以传奇结尾的书本 # ret = models.Book.objects.filter(title__endswith='传奇').values('title') # 查找作者名字里面带"张"字的作者 # ret = models.Author.objects.filter(name__contains="张") # 查找年龄大于30岁的作者 # ret = models.Author.objects.filter(age__gt=19) # 查找手机号是155开头的作者 # ret = models.Author.objects.filter(phone__startswith='155') # 查找手机号是155开头的作者的姓名和年龄 # ret = models.Author.objects.filter(phone__startswith='155').values('name','age') # 查找书名是"七侠五义"的书的出版社 # 基于对象查找 # ret = models.Book.objects.get(title="七侠五义").publisher # 查找书名是"武林传奇"的书的出版社所在的城市 # ret = models.Book.objects.get(title="武林传奇").publisher.city # 基于双下滑线的查找方式 # ret = models.Book.objects.filter(title="武林传奇").values("publisher__city") # 查找书名是"武林传奇"的书的出版社的名称 # ret = models.Book.objects.filter(title="武林传奇").values("publisher__name") # 查找书名是"七侠五义"的书的所有作者 # ret = models.Book.objects.filter(title='七侠五义').values('author__name').all() # 查找书名是"武林传奇"的书的作者的年龄 # ret = models.Book.objects.filter(title='七侠五义').values('author__name','author__age').all() # 查找书名是"武林传奇"的书的作者的手机号码 # ret = models.Book.objects.filter(title='武林传奇').values('author__name','author__phone') # 查找书名是"武林传奇"的书的作者的地址 # ret = models.Book.objects.filter(title='武林传奇').values('author__detail__addr') # 查找书名是"武林传奇"的书的作者的邮箱 # ret = models.Book.objects.filter(title='武林传奇').values('author__detail__email') # print(ret)