Django non-rel - 如何在模型中使用 EmbeddedModelField 创建表单?
我已经为 Django non-rel 设置了 Mongodb 作为后端。在模型中,我多次使用 EmbeddedModelField,因为我喜欢非关系数据库的这些概念。但是,当涉及到渲染表单时。我被卡住了,
我创建了 Form 作为 Django 的正常形式,但 django 在标题栏中显示类型错误 {model} 。
有谁知道如何在 Django non-rel 中为 EmbeddedModelField 创建表单字段?
I have setup for Django non-rel with Mongodb as backend. In models, I used EmbeddedModelField for quite a few times as I love those concepts of Non relational DBs. But, when it comes to rendering forms. I got stuck,
I created Form as normal form of Django but django showing Type error {model} in the title bar.
Has anybody know how can I create form fields for EmbeddedModelField in Django non-rel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自己实现一个formfield类就可以了。
实现表单字段是小菜一碟,你只需要在类中实现这两个方法:
to_python(self, value)
prepare_value(self, value)
如果你通过继承旧的 formfield 类来实现它,你可以使用该表单字段上附加的小部件。 (widget是指通过模板系统将UI渲染在网页上)
formfield实现参考:How to use ListFields in Django's admin
并且您可以通过覆盖表单字段的原始小部件来实现您自己的小部件。
例如,查看 django 文档: Django 文档 - 小部件
Just implement a formfield class by yourself.
implementing a formfield is a piece of cake, you just need to implement these two methods in the class:
to_python(self, value)
prepare_value(self, value)
If you implement it with inheritance of old formfield class, you could use the widget attached on that formfield. (widget means the UI will be rendered on the webpage by template system)
Reference for implementing formfield: How to use ListFields in Django's admin
And you could implement your own widget by overriding the original widget of a formfield.
For instance, take a look at django documentations: Django docs - widgets