前言
django在使用ORM查询数据库的时候,经常用到django的模型类调用objects属性,再调用.all() .filter()查询。
这里的objects到底是什么呢?objects属性实际上是models.Manager()的实例对象。
objects属性
models.py 设计一个 Teacher 模型类
class Teacher(models.Model):
"""老师表"""
name = models.CharField(max_length=30)
age = models.IntegerField(blank=True, null=True)
tel = models.CharField(max_length=30)
is_delete = models.CharField(max_length=10, default=0, blank=True)
def __str__(self):
return self.name
使用objects属性查询
D:\MyDjango>python manage.py shell
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from yoyo.models import Teacher
>>> Teacher.objects.all()
<QuerySet [<Teacher: 悠悠老师>, <Teacher: 张三老师>, <Teacher: 李四老师>, <Teacher: 王五老师>]>
>>>
模型管理器(models.Manager)
如果我们在Teacher 类里面定义一个模型管理器(models.Manager)
class Teacher(models.Model):
"""老师表"""
name = models.CharField(max_length=30)
age = models.IntegerField(blank=True, null=True)
tel = models.CharField(max_length=30)
is_delete = models.CharField(max_length=10, default=0, blank=True)
def __str__(self):
return self.name
# 自定义一个obj属性
obj = models.Manager() # 自定义模型管理对象
Teacher 类有了 obj 属性,就可以 Teacher.obj 调用了
D:\MyDjango>python manage.py shell
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from yoyo.models import Teacher
>>> Teacher.obj.all()
<QuerySet [<Teacher: 悠悠老师>, <Teacher: 张三老师>, <Teacher: 李四老师>, <Teacher: 王五老师>]>
>>> Teacher.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object ‘Teacher‘ has no attribute ‘objects‘
>>>
当设置了obj属性后,就不能用原来的objects属性调用了
重写查询all()
自定义管理器类主要用于两种情况:
1.修改原始查询集,重写all()方法
2.向管理器类中添加额外的方法,如向数据库中插入数据。
先看查询all()的时候,删选出is_delete字段为0的数据,默认不查询is_delete为1的
>>> Teacher.obj.filter(is_delete=‘0‘).values()
<QuerySet [{‘id‘: 1, ‘name‘: ‘悠悠老师‘, ‘age‘: 18, ‘tel‘: ‘12313231‘, ‘is_delete‘: ‘0‘}, {‘id‘: 3, ‘name‘: ‘李四老师‘, ‘age‘: 22, ‘tel‘: ‘1121212‘, ‘is_delete‘: ‘0‘}]>
于是可以重写all()方法
class TeacherModelManager(models.Manager):
def all(self):
# 调用父类的成员语法为:super().方法名
# 设置只返回未删除的数据
return super().all().filter(is_delete=‘0‘)
Teacher类定义objects属性
class Teacher(models.Model):
"""老师表"""
name = models.CharField(max_length=30)
age = models.IntegerField(blank=True, null=True)
tel = models.CharField(max_length=30)
is_delete = models.CharField(max_length=10, default=0, blank=True)
def __str__(self):
return self.name
# 自定义一个obj属性
objects = TeacherModelManager() # 自定义模型管理对象
于是调用all方法查询的时候就会带着条件去查询了
D:\MyDjango>python manage.py shell
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from yoyo.models import Teacher
>>> Teacher.objects.all().values()
<QuerySet [{‘id‘: 1, ‘name‘: ‘悠悠老师‘, ‘age‘: 18, ‘tel‘: ‘12313231‘, ‘is_delete‘: ‘0‘}, {‘id‘: 3, ‘name‘: ‘李四老师‘, ‘age‘: 22, ‘tel‘: ‘1121212‘, ‘is_delete‘: ‘0‘}]>
>>>
参考文档:https://docs.djangoproject.com/zh-hans/2.1/topics/db/managers/