模型 Meta class 与模型形式 Meta class 有何不同?

How is model Meta class different from model form Meta class?

我想了解我们在模型中使用的元类。我在文档中找到了它。我记得也以模型形式添加元类。看起来模型元类和模型形式元类是不同的。它们有何不同以及模型形式中的元选项是什么。

在认识论中,meta 是一个(古)希腊词,意思是 about。因此,它是一个 class 说 关于 模型或 ModelForm 的东西。名字基本上是他们唯一的共同点。

模型的Metaclass会指定模型的详细名称等,相应数据库中定义的约束和索引table等。Django文档有一个 section that lists all the Meta options for a model.

另一方面,ModelFormMeta 将向 ModelForm 解释它应该如何为给定模型构建表单。通常,Meta 定义模型,ModelFormfieldsexclude 一起构造模型,分别指定 include/exclude 的字段。此外 Overriding default fields section of the documentation lists all other Meta options, where a user can (slightly) alter the way how the fields are defined in the ModelForm. The source code [GitHub] 还列出了 ModelFormMeta 的所有选项:

class ModelFormOptions:
    def __init__(self, options=None):
        self.<strong>model</strong> = getattr(options, 'model', None)
        self.<strong>fields</strong> = getattr(options, 'fields', None)
        self.<strong>exclude</strong> = getattr(options, 'exclude', None)
        self.<strong>widgets</strong> = getattr(options, 'widgets', None)
        self.<strong>localized_fields</strong> = getattr(options, 'localized_fields', None)
        self.<strong>labels</strong> = getattr(options, 'labels', None)
        self.<strong>help_texts</strong> = getattr(options, 'help_texts', None)
        self.<strong>error_messages</strong> = getattr(options, 'error_messages', None)
        self.<strong>field_classes</strong> = getattr(options, 'field_classes', None)