Django Admin:当我单击外键旁边的添加按钮(+)时,想要预填充一些字段 [英] Django Admin: want to prepopulate some fields when I click the add button (+) next to a foreign key

查看:285
本文介绍了Django Admin:当我单击外键旁边的添加按钮(+)时,想要预填充一些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django Admin中,当我单击下拉列表旁边的添加(+)按钮时,我想使用当前记录中的数据填充外键记录的字段。

In the Django Admin I want to use data from the current record to populate fields for a foreign key record when I click the add (+) button next to the drop-down list.

例如,我正在查看 X 的实例,该实例的字段为 a b c 和外键 Y
Y 还具有 a b 和 c ,因此当我点击添加 X 实例上的按钮,我想要 a b 和新 Y 实例的 c 字段,将使用中的值填充X 实例。

For example, I am viewing an instance of X which has fields for a, b, and c and a foreign key Y. Y also has fields for a, b, and c, so when I click on the "add" button on the X instance, I want the a, b, and c fields for the new Y instance to be populated with the values from the X instance.

很明显,如果我只想添加新的Y,则这些字段不会预先填充。在管理员中!

Obviously these fields would not be pre-populated if I just go to "add new Y" in the admin!

我搜索了很多内容都没有用!

I have searched quite a bit to no avail!

有人成功了吗?

(我将django 1.3与Python 2.6配合使用;我认为这里不需要任何代码段。)

(I'm using django 1.3 with Python 2.6; I don't think any code snippets are required here.)

推荐答案

抱歉,可能为时已晚,但我认为这就是答案

Sorry, may be a bit too late but I think this is the answer

admin中Django支持url参数。您可以这样做

Django support url param in admin. You can do like this

http://localhost/admin/app/model/add/?model_field=value

然后您可以添加外键,方法是将model_field替换为外键名称,并将值替换为ID。希望这对其他人有帮助:D

then you can add foreign key by replace model_field with your foreign key name and value with the ID. Hope this help someone else :D

这篇关于Django Admin:当我单击外键旁边的添加按钮(+)时,想要预填充一些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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