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):
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):
context = super(RolesWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'results':
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)
context.update({'table': table})
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):
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"))
role.role_rights_new_style.clear()
for each_right in wizard.get('role_rights_new_style'):
RightsRoles.objects.create(role=role, right=each_right,)
for field, value in self.get_cleaned_data_for_step('details'):
setattr(role, field, value)
role.save()
return HttpResponseRedirect('/login/maintenance/roles/wizard/choice/')