class BookManager(models.Manager):
# 改变查询集的结果集
def all(self):
books = super().all() # QuerySet
books = books.filter(isDelete=False)
return books
# 封装函数:操作模型类对应的数据表(增删改查)
def create_book(self, title, author):
# book = BookInfo() # 另一个模型类
model_class = self.model # 可以获取当前模型类名,代替上一条代码
book = model_class
book.title = title
book.author = author
book.save()
return book
class BookInfo(models.Model):
title = models.CharField(max_length=20)
author = models.CharField(max_length=10)
objects = BookManager() # 自定义BookManager类的对象
==================================================================
# objects 是自己定义的,并非模型类里面的默认的 objects
# 显示的结果将是未被逻辑删除的数据
>>> BookInfo.objects.all()
>>> BookInfo.objects.create_book('了不起的盖茨比', '菲茨杰拉德')