I am using Django which allows people to add extra parameters to a class by using class Meta
.
我正在使用Django,它允许人们使用类元来为类添加额外的参数。
class FooModel(models.Model):
...
class Meta:
...
The only thing I found in Python's documentation was:
我在Python文档中发现的唯一一件事是:
class FooMetaClass(type):
...
class FooClass:
__metaclass__ = FooMetaClass
However, I don't think this is the same thing.
然而,我不认为这是一回事。
4 个解决方案
#1
150
You are asking a question about two different things:
你问的是两个不同的问题:
-
Meta
inner class in Django models:Django模型中的元内部类:
This is just a class container with some options (metadata) attached to the model. It defines such things as available permissions, associated database table name, whether the model is abstract or not, singular and plural versions of the name etc.
这只是一个类容器,带有一些附加到模型的选项(元数据)。它定义了可用的权限、关联的数据库表名、模型是否抽象、名称的单数和复数形式等等。
Short explanation is here: Django docs: Models: Meta options
简短的解释在这里:Django文档:模型:元选项。
List of available meta options is here: Django docs: Model Meta options
可用的元选项列表在这里:Django文档:模型元选项。
-
Metaclass in Python:
Python中的元类:
The best description is here: What is a metaclass in Python?
最好的描述是:什么是Python中的元类?
#2
34
Extending on Tadeck's Django answer above, the use of 'class Meta:' in Django is just normal Python too.
在Tadeck的Django回答上面,使用“class Meta:”在Django中也是正常的Python。
The internal class is a convenient namespace for shared data among the class instances (hence the name Meta for 'metadata' but you can call it anything you like). While in Django it's generally read only configuration stuff, there is nothing to stop you changing it:
内部类是在类实例之间共享数据的一个方便的名称空间(因此,“元数据”的名称为Meta,但您可以将其称为任何您喜欢的名称)。在Django中,它通常只读取配置文件,没有什么可以阻止你改变它:
In [1]: class Foo(object):
...: class Meta:
...: metaVal = 1
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()
In [4]: f1.Meta.metaVal
Out[4]: 1
In [5]: f2.Meta.metaVal = 2
In [6]: f1.Meta.metaVal
Out[6]: 2
In [7]: Foo.Meta.metaVal
Out[7]: 2
You can explore it in Django directly too e.g:
你可以直接在Django中进行研究
In [1]: from django.contrib.auth.models import User
In [2]: User.Meta
Out[2]: django.contrib.auth.models.Meta
In [3]: User.Meta.__dict__
Out[3]:
{'__doc__': None,
'__module__': 'django.contrib.auth.models',
'abstract': False,
'verbose_name': <django.utils.functional.__proxy__ at 0x26a6610>,
'verbose_name_plural': <django.utils.functional.__proxy__ at 0x26a6650>}
However, in Django you are more likely to want to explore the _meta attribute which is an Options object created by the model metaclass when a model is created. That is where you'll find all of the django class 'meta' information. In Django, Meta is just used to pass information into the process of creating the _meta Options object.
然而,在Django中,您更可能想要探索_meta属性,它是在创建模型时由模型元类创建的一个选项对象。在这里,您将找到所有django类的元信息。在Django中,Meta只是用来将信息传递到创建_meta选项对象的过程中。
#3
19
Django's Model
class specifically handles having an attribute named Meta
which is a class. It's not a general Python thing.
Django的模型类专门处理有一个名为Meta的属性,它是一个类。它不是一般的Python语言。
Python metaclasses are completely different.
Python的元类是完全不同的。
#4
2
Answers that claim Django model's Meta
and metaclasses are "completely different" are misleading answers.
声称Django模型的元类和元类的答案是“完全不同的”,这是错误的答案。
The construction of Django model class objects (that is to say the object that stands for the class definition itself; yes, classes are also objects) are indeed controlled by a metaclass called ModelBase
, you can see that code here:
Django模型类对象的构造(即表示类定义本身的对象;是的,类也是对象)实际上是由一个名为ModelBase的元类控制的,您可以看到这里的代码:
https://github.com/django/django/blob/master/django/db/models/base.py#L61
https://github.com/django/django/blob/master/django/db/models/base.py L61
And one of the things that ModelBase
does is to create the _meta
attribute on every Django model which contains validation machinery, field details, saving machinery and so forth. And, during this operation, anything that is specified in the model's inner Meta
class is read and used within that process.
ModelBase所做的一件事是在每个Django模型上创建_meta属性,它包含验证机制、字段细节、保存机制等等。在此操作过程中,模型内部元类中指定的任何内容都将在该过程中读取和使用。
So, while yes, in a sense Meta
and metaclasses are different 'things', within the mechanics of Django model construction they are intimately related; understanding how they work together will deepen your insight into both at once.
因此,在某种意义上,元类和元类是不同的“事物”,在Django模型构建的机制中它们是密切相关的;了解他们如何一起工作将会加深你对两者的洞察力。
This might be a helpful source of information to better understand how Django models employ metaclasses.
这可能是一个有用的信息来源,以便更好地理解Django模型是如何使用元类的。
https://code.djangoproject.com/wiki/DevModelCreation
https://code.djangoproject.com/wiki/DevModelCreation
And this might help too if you want to better understand how objects work in general.
如果你想更好地理解对象是如何工作的,这也有帮助。
https://docs.python.org/3/reference/datamodel.html
https://docs.python.org/3/reference/datamodel.html
#1
150
You are asking a question about two different things:
你问的是两个不同的问题:
-
Meta
inner class in Django models:Django模型中的元内部类:
This is just a class container with some options (metadata) attached to the model. It defines such things as available permissions, associated database table name, whether the model is abstract or not, singular and plural versions of the name etc.
这只是一个类容器,带有一些附加到模型的选项(元数据)。它定义了可用的权限、关联的数据库表名、模型是否抽象、名称的单数和复数形式等等。
Short explanation is here: Django docs: Models: Meta options
简短的解释在这里:Django文档:模型:元选项。
List of available meta options is here: Django docs: Model Meta options
可用的元选项列表在这里:Django文档:模型元选项。
-
Metaclass in Python:
Python中的元类:
The best description is here: What is a metaclass in Python?
最好的描述是:什么是Python中的元类?
#2
34
Extending on Tadeck's Django answer above, the use of 'class Meta:' in Django is just normal Python too.
在Tadeck的Django回答上面,使用“class Meta:”在Django中也是正常的Python。
The internal class is a convenient namespace for shared data among the class instances (hence the name Meta for 'metadata' but you can call it anything you like). While in Django it's generally read only configuration stuff, there is nothing to stop you changing it:
内部类是在类实例之间共享数据的一个方便的名称空间(因此,“元数据”的名称为Meta,但您可以将其称为任何您喜欢的名称)。在Django中,它通常只读取配置文件,没有什么可以阻止你改变它:
In [1]: class Foo(object):
...: class Meta:
...: metaVal = 1
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()
In [4]: f1.Meta.metaVal
Out[4]: 1
In [5]: f2.Meta.metaVal = 2
In [6]: f1.Meta.metaVal
Out[6]: 2
In [7]: Foo.Meta.metaVal
Out[7]: 2
You can explore it in Django directly too e.g:
你可以直接在Django中进行研究
In [1]: from django.contrib.auth.models import User
In [2]: User.Meta
Out[2]: django.contrib.auth.models.Meta
In [3]: User.Meta.__dict__
Out[3]:
{'__doc__': None,
'__module__': 'django.contrib.auth.models',
'abstract': False,
'verbose_name': <django.utils.functional.__proxy__ at 0x26a6610>,
'verbose_name_plural': <django.utils.functional.__proxy__ at 0x26a6650>}
However, in Django you are more likely to want to explore the _meta attribute which is an Options object created by the model metaclass when a model is created. That is where you'll find all of the django class 'meta' information. In Django, Meta is just used to pass information into the process of creating the _meta Options object.
然而,在Django中,您更可能想要探索_meta属性,它是在创建模型时由模型元类创建的一个选项对象。在这里,您将找到所有django类的元信息。在Django中,Meta只是用来将信息传递到创建_meta选项对象的过程中。
#3
19
Django's Model
class specifically handles having an attribute named Meta
which is a class. It's not a general Python thing.
Django的模型类专门处理有一个名为Meta的属性,它是一个类。它不是一般的Python语言。
Python metaclasses are completely different.
Python的元类是完全不同的。
#4
2
Answers that claim Django model's Meta
and metaclasses are "completely different" are misleading answers.
声称Django模型的元类和元类的答案是“完全不同的”,这是错误的答案。
The construction of Django model class objects (that is to say the object that stands for the class definition itself; yes, classes are also objects) are indeed controlled by a metaclass called ModelBase
, you can see that code here:
Django模型类对象的构造(即表示类定义本身的对象;是的,类也是对象)实际上是由一个名为ModelBase的元类控制的,您可以看到这里的代码:
https://github.com/django/django/blob/master/django/db/models/base.py#L61
https://github.com/django/django/blob/master/django/db/models/base.py L61
And one of the things that ModelBase
does is to create the _meta
attribute on every Django model which contains validation machinery, field details, saving machinery and so forth. And, during this operation, anything that is specified in the model's inner Meta
class is read and used within that process.
ModelBase所做的一件事是在每个Django模型上创建_meta属性,它包含验证机制、字段细节、保存机制等等。在此操作过程中,模型内部元类中指定的任何内容都将在该过程中读取和使用。
So, while yes, in a sense Meta
and metaclasses are different 'things', within the mechanics of Django model construction they are intimately related; understanding how they work together will deepen your insight into both at once.
因此,在某种意义上,元类和元类是不同的“事物”,在Django模型构建的机制中它们是密切相关的;了解他们如何一起工作将会加深你对两者的洞察力。
This might be a helpful source of information to better understand how Django models employ metaclasses.
这可能是一个有用的信息来源,以便更好地理解Django模型是如何使用元类的。
https://code.djangoproject.com/wiki/DevModelCreation
https://code.djangoproject.com/wiki/DevModelCreation
And this might help too if you want to better understand how objects work in general.
如果你想更好地理解对象是如何工作的,这也有帮助。
https://docs.python.org/3/reference/datamodel.html
https://docs.python.org/3/reference/datamodel.html