In Django, how do I mimic the two-step method of adding users through the admin for my own models?

前端 未结 1 616
臣服心动
臣服心动 2021-01-18 04:44

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 wan

相关标签:
1条回答
  • 2021-01-18 05:18

    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.

    0 讨论(0)
提交回复
热议问题