class AnimalManager(models.Manager): def get_queryset(self): # 只查询没有进行物理删除的数据 return super().get_queryset().filter(is_delete=False) # 创建对象 def create_animal(self,a_name='mouse'): a = self.model() a.a_name = a_name return a
class Animal(models.Model): a_name = models.CharField(max_length=16) is_delete = models.BooleanField(default=False) # 显性查询,自定义管理器 # a = models.Manager() # 调用自定义管理类 a = AnimalManager()
def get_animal(request): animals = Animal.a.all() for animal in animals: print(animal.a_name) return HttpResponse('查询成功!') def insert_animal(requesst): animal = Animal.a.create_animal('mouse') animal.save() return HttpResponse('插入成功!')