在 Django 中,我如何模仿通过管理员为我自己的模型添加用户的两步方法? [英] In Django, how do I mimic the two-step method of adding users through the admin for my own models?

查看:10
本文介绍了在 Django 中,我如何模仿通过管理员为我自己的模型添加用户的两步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,我想在其中制作一个自定义管理员 change_form 来模仿股票 Django 管理界面的添加用户功能的行为.也就是说,我想要一个两步操作,其中用户将首先仅输入一个设备 ID,然后被带到他可以在设备上输入信息的页面.类似于您首先在用户上输入用户名和密码,然后在下一页填写其他信息.有什么想法吗?

I have a model where I would like to make a custom admin change_form mimicking the behavior of the Add User functionality of the stock Django Admin Interface. That is, I want a two-step action where the user will first input only a device-id and then be taken to a page where he can enter information on the device. Similar to how you first enter username and password on a user and then fill in the other information on the following page. Any ideas?

推荐答案

我终于找到了 django 是怎么做到的.在适当的 ModelAdmin 中重新定义了一个 response_add 方法.这是 django 源中用户模型的链接:django.contrib.auth.admin.py.它看起来像这样:

I finally managed to find how django does it. There is a response_add method redefined inside proper ModelAdmin. Here is a link to it for the User model in django source: django.contrib.auth.admin.py. It looks like this:

def response_add(self, request, obj, post_url_continue='../%s/'):
    if '_addanother' not in request.POST and '_popup' not in request.POST:
        request.POST['_continue'] = 1
    return super(UserAdmin, self).response_add(request, obj, post_url_continue)

如果您将此方法添加到您的 ModelAdmin 类,那么它应该可以类似地工作.

If you add this method to your ModelAdmin class, then it should work similarly.

这仅涵盖管理面板中的两步保存过程,但其他功能,例如在第二步中向表单添加额外字段也可以在 django 源中挖掘出来.

That covers only two step save process in admin panel, but the other funcionalities like adding extra fields to the form on second step can also be digged out in the django source.

这篇关于在 Django 中,我如何模仿通过管理员为我自己的模型添加用户的两步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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