Create a two-step Django admin form to add an object?

Is it possible to create a two-step form to create an object in admin Django?

When the admin user visits /admin/my-app/article/add/, I would like to display some parameters. The application will then display a create page with pre-computed fields based on the selections made.

+3
source share
1 answer

You can overwrite the method add_viewin ModelAdmin( Source ) myapp.article. He is responsible for rendering the model and adding objects to the database.

When adding your functions, you probably want to keep the source code intact, and not copy / modify it.

Project

def add_view(self, request, **kwargs):
    if Stage1:
        do_my_stuff()
        return response
    else:
        return super(ModelAdmin, self).add_view(self, request, **kwargs)

. GET . , .

2

def add_view(self, request, **kwargs):
    if 'stage2' not in request.GET:
        if request.method == 'POST':
            # calculate values
            parameters = 'field1=foo&field2=bar'
            return redirect('myapp_article_add' + '?stage2=1&' + parameters)
        else:
            # prepare form for selections
            return response
    else:
        return super(ModelAdmin, self).add_view(self, request, **kwargs)

, , . add_view, , , .

+5

Source: https://habr.com/ru/post/1791102/


All Articles