模型类参考Link to this heading
本文档涵盖了 Model 类的功能。关于模型的更多信息,请参见 模型参考指南的完整列表。
属性Link to this heading
DoesNotExistLink to this heading
- exception Model.DoesNotExistLink to this definition
当没有找到预期的对象时,ORM 会引发这个异常。例如,
QuerySet.get()将在没有找到给定查找对象时引发该异常。Django 提供了一个
DoesNotExist的异常作为每个模型类的属性,用来标识找不到的对象类,允许你为某个模型类捕捉异常。该异常是django.core.exceptions.ObjectDoesNotExist的一个子类。
MultipleObjectsReturnedLink to this heading
- exception Model.MultipleObjectsReturnedLink to this definition
当给定的查找找到多个对象时,
QuerySet.get()会引发这个异常。Django 提供了一个
MultipleObjectsReturned的异常作为每个模型类的属性,用于识别发现多个对象的对象类,允许你为某个模型类捕捉异常。该异常是django.core.exceptions.MultipleObjectsReturned的一个子类。
NotUpdatedLink to this heading
- exception Model.NotUpdatedLink to this definition
This exception is raised when a forced update of a
Modelinstance does not affect any rows.Django provides a
NotUpdatedexception as an attribute of each model class to identify the class of object that could not be updated, allowing you to catch exceptions for a particular model class. The exception is a subclass ofdjango.core.exceptions.ObjectNotUpdatedand inherits fromdjango.db.DatabaseErrorfor backward compatibility reasons.
objectsLink to this heading
- Model.objectsLink to this definition
每个非抽象的
Model类必须有一个Manager实例添加到其中。Django 确保在你的模型类中至少指定了一个默认的Manager。如果你没有添加自己的Manager,Django 会添加一个属性objects,包含默认的Manager实例。如果添加自己的Manager实例属性,则不会出现默认的。考虑下面的例子:from django.db import models class Person(models.Model): # Add manager with another name people = models.Manager()