Guest User

Untitled

a guest
Sep 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. When using inline formsets with form wizards, where does formset form validation go?
  2. class Parent(models.Model):
  3. name = models.CharField(max_length=256)
  4. def __unicode__(self):
  5. return name
  6.  
  7. class Child(models.Model):
  8. name = models.CharField(max_length=256)
  9. parent = models.ForeignKey(Parent)
  10. def __unicode__(self):
  11. return name
  12.  
  13. test_forms = [['parent', ParentForm],['child', ChildFormSet]]
  14.  
  15. urlpatterns = patterns('example.views',
  16. url(r'^$', TestWizard.as_view(test_forms)),
  17. )
  18.  
  19. class ParentForm(ModelForm):
  20. class Meta:
  21. model = Parent
  22.  
  23. class ChildForm(ModelForm):
  24. class Meta:
  25. model = Child
  26. exclude = ('parent',)
  27.  
  28. ChildFormSet = inlineformset_factory(Parent, Child, extra=1)
  29.  
  30. class TestWizard(SessionWizardView):
  31. """
  32. This WizardView is used to create multi-page forms and handles all the
  33. storage and validation stuff using sessions.
  34. """
  35. #template_name = ''
  36.  
  37. # def get_template_names(self):
  38. # """
  39. # Returns a list of template names to be used for the request.
  40. # Overridden TemplateResponseMixin for specifying template for step.
  41. # """
  42. # return 'survey/forms/wizard_form.html'
  43. #
  44. # def get_context_data(self, form, **kwargs):
  45. # context = super(TestWizard, self).get_context_data(form=form, **kwargs)
  46. # return context
  47. #
  48. # def get_form_initial(self, step):
  49. # """
  50. # Returns dict (list of key, values) for initial form data.
  51. # Useful for populating form fields with data from prior form, with extra
  52. # logic for dealing with formsets.
  53. # """
  54. # return self.initial_dict.get(step, {})
  55. #
  56. # def get_form(self, step=None, data=None, files=None):
  57. # """
  58. # Constructs the form for a given step - overridden to add extra arguments
  59. # """
  60. # form = super(TestWizard, self).get_form(step, data, files)
  61. # return form
  62.  
  63. def done(self, form_list, **kwargs):
  64. return render_to_response('survey/thanks.html', {
  65. 'form_data': [form.cleaned_data for form in form_list],
  66. })
  67.  
  68. {% extends "base.html" %}
  69. {% load i18n %}
  70.  
  71. {% block head %}
  72. {{ wizard.form.media }}
  73. {% endblock %}
  74.  
  75. {% block content %}
  76. <p>DEFAULT WIZARD FORM Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
  77. <form action="" method="post">{% csrf_token %}
  78. <table>
  79. {{ wizard.management_form }}
  80. {% if wizard.form.forms %}
  81. {{ wizard.form.management_form }}
  82. {% for form in wizard.form.forms %}
  83. {{ form }}
  84. {% endfor %}
  85. {% else %}
  86. {{ wizard.form }}
  87. {% endif %}
  88. </table>
  89. {% if wizard.steps.prev %}
  90. <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
  91. <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
  92. {% endif %}
  93. <input type="submit" value="{% trans "submit" %}"/>
  94. </form>
  95. {% endblock %}
Add Comment
Please, Sign In to add comment