在Django中,如何模拟通过管理员为自己的模型添加用户的两步法? [英] In Django, how do I mimic the two-step method of adding users through the admin for my own models?
问题描述
我终于找到了django如何做。在 ModelAdmin
中重新定义了一个 response_add
方法。以下是django源中用户模型的链接: django.contrib.auth.admin.py 。看起来像这样:
def response_add(self,request,obj,post_url_continue ='.. /%s /')
如果'_addanother'不在request.POST和'_popup'不在request.POST:
request.POST ['_ continue'] = 1
返回超级(UserAdmin,self).response_add (request,obj,post_url_continue)
如果将此方法添加到您的 ModelAdmin
类,那么它应该同样工作。
这只包括管理面板中的两个步骤保存过程,但其他功能就像添加额外的字段第二步的形式也可以在django源中挖掘出来。
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?
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)
If you add this method to your ModelAdmin
class, then it should work similarly.
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屋!