1

我正在学习 Django。特别是我正在阅读有关表格的内容,但我无法理解它们是如何工作的。让我们看一下示例代码:

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

为什么要创建具有无限成员的类?没有self声明。

我查看了 Django 源代码本身:

class Form(BaseForm):
"A collection of Fields, plus their associated data."
# This is a separate class from BaseForm in order to abstract the way
# self.fields is specified. This class (Form) is the one that does the
# fancy metaclass stuff purely for the semantic sugar -- it allows one
# to define a form using declarative syntax.
# BaseForm itself has no way of designating self.fields.
__metaclass__ = DeclarativeFieldsMetaclass

你能解释一下它的作用是__metaclass__什么吗?

4

1 回答 1

0

表单字段是描述符,它们只是一个写入特定协议的类。元类是类的类型(因为类本身是对象,是从某物实例化的)。

于 2012-09-09T16:20:19.560 回答