在类似 django 的表单实现中使用元类有什么优点?
首先是一些背景知识……我正在查看表单的 Django 源代码,以了解 Django 中表单的实现(并在此过程中学习一些 Python)。 Django 使用 DeclaredMetaFields MetaClass 实现表单。
这是一个非常粗略的类似 Django 表单实现的类图(链接到 gist 中的示例代码)。
这是一个实例图。
这是一个非常粗略的实现,相同类而不求助于元类(链接到要点中的示例代码)。
我了解元类概念等,并了解 Django 代码的工作原理。现在提问。
- 除了语法优雅等明显的好处之外,元类实现还有其他好处吗?
- 是否可以在不借助 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).
And here is an instance diagram.
Here is a very crude implementation the same class without resorting to meta-classes (link to sample code in gist).
I understand the metaclass concepts etc. and understand how the Django code works. Now for the questions.
- Other than the obvious benefits such as syntactical elegance etc. are there any other benefits for the meta-class implementation?
- Is the meta-class like implementation possible without resorting to an intermediate object like BoundField?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
语法上的好处很重要。毕竟 OOP 语言中的类只是该语言的语法优势。
在您的无元类表单实现的非常粗略的实现示例中,您描述了 Dict 中的字段。好吧,您可能忽略了它实际上是 SortedDict,因为字段的顺序很重要。所以我还需要定义
fields_order
列表。下一件大事是 ModelForm。元类方法允许简单地说明我使用哪个模型以及
Meta
属性中的哪些字段,它会自动创建字段并将其映射到模型。如果没有元类方法,我可能不得不使用诸如 CreateFieldsFromModel 和 MapFieldsToModel 之类的方法。或者您可以在 __init__ 方法中为我执行此操作。但是等等,__init__
方法已经足够复杂了,有很多参数,例如data
、initial
、files
等。还有更多的东西可以在表单中配置,并且所有内容都记录在案。
所以对我来说,由于庞大的配置逻辑,看起来
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 likeCreateFieldsFromModel
andMapFieldsToModel
. Or you might do that for me in__init__
method. But wait,__init__
method is already complex enough with lots of arguments likedata
,initial
,files
, and more.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?