Django modelform:是否可以添加相关模型? [英] Django modelform: is inline adding related model possible?

查看:119
本文介绍了Django modelform:是否可以添加相关模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望这不是重复的:我找不到任何东西,但这只能意味着我不善于搜索:)



我有一个Django应用程序和工作人员已经在使用管理应用程序...以及管理目的。

我还创建了一个特定模型的快速数据输入页面,通过在基础中删除模型形式模板,以便我有:




  • 自定义字段

  • 自定义小部件

  • JavaScript客户端验证(服务器端验证当然也存在)

  • ,当然我的页面布局。



...最小的努力;我知道管理页面可以被定制以获得所有这些东西,但是由于我有这样的需求,所以在一个模型中一个操作(插入),这显然是获胜的解决方案。



问题出在这个事实,这个模型有两个 ForeignKey s应用程序中的其他模型。

模型窗体默认情况下使用相关的模型实例使用选择来实现:是否可以在我的Form / ModelForm中具有添加相关模型的实例与Django管理员默认情况下的相同方式?还是应该使用Django管理员做的事情?



这是供编辑人员使用的,当他们不想在两页之间进行往返时,我真的可以理解他们。 >

解决方案

嗯,由于管理员是用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 ForeignKeys 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.

解决方案

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.

这篇关于Django modelform:是否可以添加相关模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆