Django的Meta类如何工作? [英] How does Django's Meta class work?
问题描述
我正在使用Django,它允许人们使用 class Meta
添加额外的参数。
class FooModel(models.Model):
/ pre>
...
class Meta:
...
我在Python的文档中找到的唯一的东西是:
class FooMetaClass(type):
...
class FooClass:
__metaclass__ = FooMetaClass
但是,我不认为这是一样的。
解决方案你正在问两个不同的问题:
Meta
Django模型中的内部类:
这只是一个类容器,它附有模型的一些选项(元数据)。它定义了可用的权限,关联的数据库表名称,模型是抽象的,单个和复数个版本的名称等。
简短的解释在这里: Django文档:模型:元选项
Python中的元类:
最好的描述是这里:什么是Python中的元类?
I am using Django which allows people to add extra parameters to a class by using
class Meta
.class FooModel(models.Model): ... class Meta: ...
The only thing I found in Python's documentation was:
class FooMetaClass(type): ... class FooClass: __metaclass__ = FooMetaClass
However, I don't think this is the same thing.
解决方案You are asking a question about two different things:
Meta
inner class in Django models: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
List of available meta options is here: Django docs: Model Meta options
Metaclass in Python:
The best description is here: What is a metaclass in Python?
这篇关于Django的Meta类如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!