简体   繁体   English

在Django管理界面中插入到外键链接表

[英]Insert to foreign key-linked table in django admin interface

I'm writing a rudimentary exercise app in django, with the following table structure: 我正在django中编写一个基本的锻炼应用程序,其表结构如下:

Routine         Exercise         Segment
=====           =====            =====
routine_name    exercise_name    routine_id (fk)
routine_id      exercise_id      exercise_id (fk)
                                 order
                                 duration

* (fk) = foreign key

In practice, this looks as follows: 实际上,这看起来如下:

在此处输入图片说明

A Routine contains numerous Segments, and each segment contains one Exercise, the order number (where the segment appears in the routine – first, second, third, etc), and the duration of that segment. 例程包含许多段,每个段包含一个练习,顺序号(该段出现在例程中的位置-第一,第二,第三等)以及该段的持续时间。

My question is, is there an way to allow someone to add a new exercise type from the "Add routine" view? 我的问题是, 有没有办法允许某人从“添加常规”视图添加新的锻炼类型? Ie, if I'm making a new routine, I don't necessarily want to have to drop out and go to the Exercise table view to add a new exercise to the list. 即,如果我要制定一个新的例程,则不必一定要退出并进入“练习表”视图以将新练习添加到列表中。 Thanks! 谢谢!

The trick is to add a ModelAdmin for Exercise. 诀窍是添加用于练习的ModelAdmin。

The obvious consequence of this is that you'll get a "top level" menu option to add exercises. 这样做的明显结果是,您将获得一个“顶层”菜单选项来添加练习。 However, a side effect is that everywhere that you have a model with a foreign key to exercise (like in your Segment inline), you'll get a little green "plus" sign next to the Exercise foreign key selector. 但是,副作用是,在您拥有要使用外键进行锻炼的模型的任何地方(例如在段内联中),您都将在“锻炼”外键选择器旁边看到一个绿色的“加号”。 If you click on that plus, you'll get a popup that will let you create a new Exercise. 如果单击该加号,则会弹出一个对话框,让您可以创建一个新的练习。 The popup will have the same layout as the "Add Exercise" page you get if you go through the top level menu, but without the navigation elements. 弹出菜单的布局与您通过顶层菜单获得的“添加锻炼”页面的布局相同,但没有导航元素。

When you hit "save" on the new Exercise, the foreign key you clicked on will be updated to point at the newly added exercise instance. 当您在新的练习上单击“保存”时,您单击的外键将更新为指向新添加的练习实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM