如何扩展 django 可插拔应用程序?
假设我正在使用 django 标记应用程序,并且我决定向现有标记表单添加一个表单类。我不想将表单修补到现有的 forms.py 中,因为更新时它会被破坏。如何扩展 forms.py 以包含我的表单类?
我尝试在我的应用程序中添加一个“标记”文件夹,其中包含仅包含我的类的 forms.py,但这会破坏已安装应用程序的表单类。 (我知道这是一个渺茫的机会,只是想尝试一下)。
关于在哪里查找有关将表单类添加到现有应用程序的信息有什么建议吗?
Let's say I am using django-tagging application and I decide I want to add a form class to the existing tagging forms. I do not want to patch the form into the existing forms.py since it will be blown out when updated. How do I extend forms.py to include my form class?
I tried adding a "tagging" folder in my app with a forms.py that only included my class, but that breaks the installed app's form classes. (I know it was a long shot, just thought I would give it a try).
Any suggestions on where to look for info on adding a form class to an existing application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的答案是,您从表单类继承,创建您自己的修改,并使用您的修改版本。
因此,您可以从自己的 forms.py 导入,而不是从 taggit 导入
。当我想要装饰/增强现有应用程序的功能时,我已经这样做过很多次了。缺点是,如果文档很差,您必须深入研究源代码以找出调用堆栈是什么以及要修改的功能的继承树。
这种方法的缺点是您无法修改贡献的应用程序的行为。因此,如果您希望 taggit 的某些内部功能使用您的自定义代码 - 这很难实现。然而,这种情况非常罕见。
如果您确实想这样做(就像我曾经做过的那样),您可以克隆源代码并维护您自己的副本。使用您的修改创建一个分支,并确保编写良好的测试。
然后,您应该将此代码作为其自己的应用程序添加到您的 django 项目中;由于 Python 的查找规则 - 它将找到您的本地副本并使用它,而不是全局 site-packages 目录中的副本。
如果更新了上游,那么您的负担就是通过确保更新/变基时测试不会失败来维护您的副本。
The simple answer is, you inherit from the form classes, create your own modifications, and use your modified version.
So, instead of importing from taggit, you would import from your own forms.py
I have done this many times when I want to decorate/enhance an existing app's functionality. The downside is if the the documentation is poor, you have to dig through the source to find out what the call stack is and the inheritance tree for the functionality you want to modify.
The downside of this approach is you cannot modify the contributed application's behavior. So if you want some internal functionality of taggit to use your custom code - this is difficult to implement. However, this is a very rare case.
Should you really want to do that (as I had to do once), you can clone the source and maintain your own copy. Create a branch with your modifications, and make sure you write good tests.
You should then add this code to your django project as its own application; due to Python's lookup rules - it will find your local copy and use it instead of the one from the global site-packages directory.
If the upstream is updated, your burden is then to maintain your copy by making sure tests don't fail when you update/rebase.
Jacob Kaplan-Moss,Django 的原始开发者,回答了您的问题他的演讲《现实世界中的 Django》第 185 页到第 203 页的摘要(因为你的问题一开始就是抽象的)。
但是,为了应用该建议,插件开发人员必须按照指南编写其应用程序。
Jacob Kaplan-Moss, the original developer of Django, answers your question in the abstract (since your question was abstract in the first place) on pages 185 through 203 of his presentation, "Django In the Real World."
However, in order for that advice to apply, the plug-in developer must have written his app in accordance with the guidelines.