Django modelform:是否可以内联添加相关模型?
我真的希望这不是重复的:我找不到任何东西,但这可能意味着我不擅长搜索:)
我有一个 Django 应用程序,并且工作人员已经在使用管理应用程序......好吧,管理目的。
我还为特定模型创建了一个快速数据输入页面,基本上是通过在基本模板中删除模型表单来创建的,这样我就拥有:
- 自定义字段
- 自定义小部件
- javascript客户端验证(当然也存在服务器端验证)
- 和当然是我的页面布局。
...以最小的努力;我知道可以自定义管理页面来获取所有这些内容,但由于我需要在 one 模型上进行 one 操作(插入),所以这显然是获胜的选择解决方案。
问题的根源是该模型对应用中的其他模型有两个 ForeignKey
。
模型表单默认使用带有相关模型实例的 select
来呈现此内容:是否可以在我的 Form/ModelForm 中添加相关模型的实例与 Django 管理员默认的方式相同吗?或者我应该使用 Django 管理来完成这件事?
这是供编辑人员使用的,当他们说不想在两个页面之间往返时,我真的能理解他们。
I really hope this is not a duplicate: I couldn't find anything but that could just mean I'm not good at searching :)
I have a Django app, and the staff is already using the admin app for... well, administration purposes.
I also created a quick data entry page for a specific model, created substantially by dropping a modelform inside the base template, so that I have:
- custom fields
- custom widgets
- javascript client-side validation (server-side validation is of course also present)
- and of course my page layout.
...with minimal effort; I know that the admin page can be customized to obtain all this things, but since I have these needs for one operation (insertion) on one model, this was clearly the winning solution.
The problem arises from the fact that this model has two ForeignKey
s to other models in the app.
The modelform render this by default with a select
with the related model instances: is it possible to have in my Form/ModelForm the ability to add an instance of the related model in the same way that the Django admin does by default? Or should I do over the thing using the Django admin?
This is intended for use by the editorial staff, and I really can understand them when they say they don't want to make round trips between two pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,由于管理是用 Django 本身编写的,因此可以在您自己的代码中执行您可以在 Django 中执行的任何操作。
我认为您只需要阅读 有关内联表单集的文档< /a>.
Well, since the admin is written in Django itself, it is possible to do anything in your own code that you can do in Django.
I think you just need to read the documentation on inline formsets.