当我们有多个对象管理器时,Django 如何评估对象管理器?
How Django evaluates object managers when we have multiple ones?
考虑 Django ORM 中的以下对象:
class Post(models.Model):
...
published = PublishedPostManager()
objects = models.Manager()
现在在Django admin中,只显示已发布的。我们可以改变行为,让 Django ORM 默认使用对象而没有任何顺序吗?
如docs
所述:
If you use custom Manager objects, take note that the first Manager Django encounters (in the order in which they’re defined in the model) has a special status. Django interprets the first Manager defined in a class as the “default” Manager
所以默认使用objects
,放在published
之前:
class Post(models.Model):
...
objects = models.Manager()
published = PublishedPostManager()
或将其定义为模型中的默认管理器Meta
:
class Post(models.Model):
...
published = PublishedPostManager()
objects = models.Manager()
class Meta:
default_manager_name = 'objects'
考虑 Django ORM 中的以下对象:
class Post(models.Model):
...
published = PublishedPostManager()
objects = models.Manager()
现在在Django admin中,只显示已发布的。我们可以改变行为,让 Django ORM 默认使用对象而没有任何顺序吗?
如docs
所述:
If you use custom Manager objects, take note that the first Manager Django encounters (in the order in which they’re defined in the model) has a special status. Django interprets the first Manager defined in a class as the “default” Manager
所以默认使用objects
,放在published
之前:
class Post(models.Model):
...
objects = models.Manager()
published = PublishedPostManager()
或将其定义为模型中的默认管理器Meta
:
class Post(models.Model):
...
published = PublishedPostManager()
objects = models.Manager()
class Meta:
default_manager_name = 'objects'