Django modelform: is inline adding related model possible?

笑着哭i 提交于 2019-12-10 21:48:19

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2207464/django-modelform-is-inline-adding-related-model-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!