Django 中模型 类 和模型实例之间的区别(文档)
Difference between model classes and model instances in Django (documentation)
我在阅读 Django 文档时遇到了这条线。
Managers are only accessible via model classes, not the model
instances.
这行是什么意思?我无法理解这一点。我知道什么是模型 类(如果我没记错的话,代表数据库中的 Table)。模型实例是否与我们有时所说的 "objects" 相同?
这行到底是什么意思?这是一些 OOP 概念还是只是 Django?
假设您有一个型号 X:
class X(models.Model):
pass
现在如果要访问Manager方法,需要这样访问:
X.objects.all()
但是下面一行行不通:
> x = X() # model instance
> x.save()
> x.objects << will throw error
仅供参考:它是特定于 Django 的,而不是 OOP。
我在阅读 Django 文档时遇到了这条线。
Managers are only accessible via model classes, not the model
instances.
这行是什么意思?我无法理解这一点。我知道什么是模型 类(如果我没记错的话,代表数据库中的 Table)。模型实例是否与我们有时所说的 "objects" 相同?
这行到底是什么意思?这是一些 OOP 概念还是只是 Django?
假设您有一个型号 X:
class X(models.Model):
pass
现在如果要访问Manager方法,需要这样访问:
X.objects.all()
但是下面一行行不通:
> x = X() # model instance
> x.save()
> x.objects << will throw error
仅供参考:它是特定于 Django 的,而不是 OOP。