在类似 django 的表单实现中使用元类有什么优点?

发布于 2024-10-27 13:16:49 字数 785 浏览 3 评论 0原文

首先是一些背景知识……我正在查看表单的 Django 源代码,以了解 Django 中表单的实现(并在此过程中学习一些 Python)。 Django 使用 DeclaredMetaFields MetaClass 实现表单。

这是一个非常粗略的类似 Django 表单实现的类图(链接到 gist 中的示例代码)。

ContactForm 的类似 Django 表单的实现 - 类图

这是一个实例图。

ContactForm 的类似 Django 表单的实现 - 实例图

这是一个非常粗略的实现,相同类而不求助于元类(链接到要点中的示例代码)。

ContactForm 的简单粗暴实现

我了解元类概念等,并了解 Django 代码的工作原理。现在提问。

  1. 除了语法优雅等明显的好处之外,元类实现还有其他好处吗?
  2. 是否可以在不借助 BoundField 这样的中间对象的情况下实现类似元类的实现?

First a little background ... I was going over the Django source code for forms to understand the implementation of forms in Django (and to learn some Python along the way). Django implements forms using a DeclaredMetaFields MetaClass.

Here is a very crude class diagram of a Django-like form implementation (link to sample code in gist).

Django-form-like implementation of ContactForm - Class Diagram

And here is an instance diagram.

Django-form-like implementation of ContactForm - Instance Diagram

Here is a very crude implementation the same class without resorting to meta-classes (link to sample code in gist).

A simple and crude implementation of ContactForm

I understand the metaclass concepts etc. and understand how the Django code works. Now for the questions.

  1. Other than the obvious benefits such as syntactical elegance etc. are there any other benefits for the meta-class implementation?
  2. Is the meta-class like implementation possible without resorting to an intermediate object like BoundField?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-11-03 13:16:49

语法上的好处很重要。毕竟 OOP 语言中的类只是该语言的语法优势。

在您的无元类表单实现的非常粗略的实现示例中,您描述了 Dict 中的字段。好吧,您可能忽略了它实际上是 SortedDict,因为字段的顺序很重要。所以我还需要定义 fields_order 列表。

下一件大事是 ModelForm。元类方法允许简单地说明我使用哪个模型以及 Meta 属性中的哪些字段,它会自动创建字段并将其映射到模型。如果没有元类方法,我可能不得不使用诸如 CreateFieldsFromModel 和 MapFieldsToModel 之类的方法。或者您可以在 __init__ 方法中为我执行此操作。但是等等,__init__ 方法已经足够复杂了,有很多参数,例如 datainitialfiles 等。

class MyForm(Form):
    fields = {
        'one': ...
        ...
    }
    fields_order = [...]
    model_fields = ???

Class MyForm2(MyForm):
    fields = MyForm.fields + {...}

# ... hey this API sucks! I think I'll go with another framework.

还有更多的东西可以在表单中配置,并且所有内容都记录在案。

所以对我来说,由于庞大的配置逻辑,看起来Form只是要求通过定义对象和工厂逻辑来实现。 Python 及其元类来向用户隐藏工厂。它很酷,因为它让初学者思考得更少。

是的,它的语法糖无处不在,而且都是为了制作易于使用的 API。

是的,可以不使用元类/BoundField 或其他任何东西。最后,可以在一个函数中编写表单的所有实现,并将所有定义放在一个大字典(或 xml?)中,但这会易于使用、易于理解、易于扩展吗?

Well syntactical benefits matters a lot. After all even classes in OOP languages is just a syntactical benefit of the language.

In your example of very crude implementation of meta-class-less form implementation you describe fields in Dict. Well you might have overlooked that it is actually SortedDict, because ordering of fields matters. So I'll need to define fields_order list as well.

Next big thing is ModelForm. Meta-class approach allows to simply say which Model do I use and which fields in Meta attribute and it automatically creates and maps fields to model. Without Metaclass approach I would probably have to use something like CreateFieldsFromModel and MapFieldsToModel. Or you might do that for me in __init__ method. But wait, __init__ method is already complex enough with lots of arguments like data, initial, files, and more.

class MyForm(Form):
    fields = {
        'one': ...
        ...
    }
    fields_order = [...]
    model_fields = ???

Class MyForm2(MyForm):
    fields = MyForm.fields + {...}

# ... hey this API sucks! I think I'll go with another framework.

And there are many more things which can be configured in forms and everything is documented.

So for me, because of huge configuration logic, it looks like Form just asks to be implemented through definition-object and factory-logic. And here comes python with its metaclasses to hide the factory from the user. And it is cool because it makes beginners to think less.

And well yea its syntactical sugar all around and its all about making easy to use API.

And yes it is possible not to use Metaclasses/BoundField or whatever else. In the end it is possible to write all implementation of forms in one function and have all definition in one big dict (or xml?) But will that be easy to use, easy to understand, easy to extend?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文