复制 Django Admin 的“添加”按钮

发布于 2024-12-06 08:20:16 字数 679 浏览 0 评论 0 原文

假设我有以下两个模型,每个模型都有自己的表单来创建模型记录。

例如:

Business_Client Model:
   busName field - CharField
   mainContact field - ForeignKey(Contacts)

Contacts Model:
    firstName field - CharField
    lastName field - CharField

当用户想要创建新业务时,他们必须从下拉菜单中选择“主要联系人”。但是,如果联系人不在列表中,则他们必须先创建记录,然后返回并重新开始创建业务 再次录制。

管理界面通过下拉菜单旁边的小 + 按钮使这一切变得简单,该按钮会将您带到 Contact 表单,填写后点击 Save< /code> 然后将您返回Business 表单,mainContact 字段已选择为新的创建了联系人记录。

我该怎么办?!我一直在谷歌上搜索,但没有找到。有人有一些好的链接/教程可以让我继续吗?

谢谢!

Let's say I have the following two models, each with their own form to create model records.

For example:

Business_Client Model:
   busName field - CharField
   mainContact field - ForeignKey(Contacts)

Contacts Model:
    firstName field - CharField
    lastName field - CharField

When the user wants to create a new business, they will have to select a "Main Contact" from a drop down menu. However, if the contact is not in the list, they have to create that record first, then come back, and re-start creating the business record again.

The admin interface makes this easy by having the little + button beside the drop down menu which takes you to the Contact form, you fill it out, hit Save which then brings you back to the Business form, with the mainContact field already selected to your newly created Contact record.

How do I do this!?! I have been searching around Google and am coming up short. Anyone have some good links/tutorials that would get me going?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一束光,穿透我孤独的魂 2024-12-13 08:20:16

我从来没有这样做过,但想一想:

您有一个视图

/add/business/

,其中包含姓名字段和联系人字段(旁边有一个加号)。加号只是一个链接,通过 javascript 创建新的弹出窗口,该窗口指向

/add/contact/

并具有 javascript 回调。当表单提交、验证并放入数据库时​​,窗口将关闭,ID/名称将传回原始表单并自动输入到字段中。

这似乎是 django 管理员的做法。您可以查看 django 管理员自己使用的小部件:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/widgets.py#L218

的渲染函数

具有 html: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/widgets.py#L249

这表明它只是一个带有 onlick javascript 弹出窗口的锚链接指向相关的添加视图。提交表单后,值将被传回。

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js#L55

I've never done it, but thinking about it:

You have a view

/add/business/

with a field for name, and a field for contact (with a little plus beside it). The plus is simply a link that creates new popup window via javascript that points to

/add/contact/

and has a javascript callback. When the form is submitted, validated and put into the DB, the window closes and the id/name is passed back to the original form and auto entered in the field.

This seems to be how the django admin does it. You can look at the widget that the django admin uses yourself:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/widgets.py#L218

The render function that has the html:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/widgets.py#L249

which shows that it's simply an anchor link with an onlick javascript popup pointing to the relvant add view. Once the form is submitted the values are passed back.

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js#L55

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文