Need to clarify the use of Django 1.4 Form Wizards, in particular pre-filling and saving

We create a wizard using the function to create a new form of Django 1.4.
The documents on this issue are very short, and we cannot find any additional examples. We use a name wizard with the name (required to support the / datagrid used) and the session server. The wizard is intended for editing roles and related rights and was created to provide functionality for adding and editing. We do this by asking the user in the first step if he wants to add or change.

The next step depends on this choice; If the user wants to edit, there is a search screen, and then a listview / datagrid that displays the results. Then the user can select one of the results and go to the detail screen, and then FilteredSelectMultiple , allowing him / her to associate the rights to this role.

If the user wants to add a new role, the search and results screens are skipped and the user goes directly to the details screen, followed by the link screen.
All this works very well using the dict clause in urls.py, but we are wondering about the general functionality:

When a previously defined role is selected, how can we fill in the data and the link screen with the relevant data?

We create an instance of the role object and pass it somehow to the two forms, if so, where do we create it, and does it need to be done for each form separately (which seems a bit higher)?

When saving, is it common practice to create another instance of the role object, add form data to it, and save or reuse the object used in the forms?

We tried to overload get_form_instance to return role instances, and we looked at instance instance in documents, but this seems like the wrong approach, and there are no examples that can be found on the Internet, and we are not even sure that this is used to prefill data or even if we are on the right track.

, , , , -, , . . , INSERT UPDATE, , .

- , .

wizardview views.py :

class RolesWizard(NamedUrlSessionWizardView):

def get_template_names(self):
    # get template for each step...
    if self.steps.current == 'choice':
        return 'clubassistant/wizard_neworeditrole.html'
    if self.steps.current == 'search':
        return 'clubassistant/wizard_searchrole.html'
    if self.steps.current == 'results':
        return 'clubassistant/wizard_pickrole.html'
    if self.steps.current == 'details':
        return 'clubassistant/wizard_detailsrole.html'
    elif self.steps.current == 'rights':
        return 'clubassistant/wizard_roles.html'

def get_context_data(self, form, **kwargs):
    # get context data to be passed to the respective templates
    context = super(RolesWizard, self).get_context_data(form=form, **kwargs)

    # add the listview in the results screen
    if self.steps.current == 'results':
        # get search text from previous step
        cleaned_data = self.get_cleaned_data_for_step('search')
        table = RolesTable(Roles.objects.filter(
            role_name__contains=cleaned_data['searchrole'])
        )
        RequestConfig(self.request, paginate={
            "per_page": 4,
            }).configure(table)
        # add the listview with results
        context.update({'table': table})

    # add a role instance based on the chosen primary key
    if self.steps.current == 'rights':
        cleaned_data = self.get_cleaned_data_for_step('results')
        role_id = cleaned_data['role_uuid']
        role = get_object_or_404(Roles, pk=role_id)
        context.update({'role': role})

    return context

def done(self, form_list, **kwargs):
    # this code is executed when the wizard needs to be completed

    # combine all forms into a single dictionary
    wizard = self.get_all_cleaned_data()

    if wizard.get("neworeditrole")=="add":
        role = Roles()
    else:
        role = get_object_or_404(Roles, pk=wizard.get("role_uuid"))

    # many-to-many rights/roles
    role.role_rights_new_style.clear()
    for each_right in wizard.get('role_rights_new_style'):
        RightsRoles.objects.create(role=role, right=each_right,)

    # other properties
    for field, value in self.get_cleaned_data_for_step('details'):
        setattr(role, field, value)

    role.save()

    # return to first page of wizard...
    return HttpResponseRedirect('/login/maintenance/roles/wizard/choice/')
+5
2

:

get_form(), . ModelForms:

class Wizard1(models.ModelForm): 
    class Meta:
        model = MyModel
        fields = ('field0', 'model0')
class Wizard2(models.ModelForm): 
    class Meta:
        model = MyModel
        excludes = ('field0', 'model0')

SessionWizardView:

class MyWizard(SessionWizardView):
    def get_form(self, step=None, data=None, files=None):
        form = super(ExtensionCreationWizard, self).get_form(step, data, files)

        if step is not None and data is not None:
            # get_form is called for validation by get_cleaned_data_for_step()
            return form

        if step == "0":
            # you can set initial values or tweak fields here

        elif step == "1":
            data = self.get_cleaned_data_for_step('0')
            if data is not None:
                form.fields['field1'].initial = data.get('field0')
                form.fields['field2'].widget.attrs['readonly'] = True
                form.fields['field3'].widget.attrs['disabled'] = True
                form.fields['model1'].queryset = Model1.objects.filter(name="foo")

        return form

1. 0 ( get_form() 0, ), , 0.

, . , ChoiceField , . , ... readonly ChoiceField. , .

+2

, . , . , :

def process_step(self, request, form, step):
  request.session['form_list'] = self.form_list
  request.session['initial'] = self.initial

, , , :

def dynamic_wizard(request):
  if not request.session.get('form_list'):
        form = Wizard([Form1])
    else:
        form = Wizard(request.session.get('form_list'), initial = request.session['initial'])
    return form(context=RequestContext(request), request=request)
+1

All Articles