Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple formularios en vista
# PriorityFormSet = formset_factory(PriorityForm, extra=5, can_delete=True)
PriorityFormSet = modelformset_factory(Priority, form=PriorityForm, extra=3, can_delete=True)
# TicketStatusFormSet = formset_factory(TicketStatusForm, extra=3, can_delete=True)
TicketStatusFormSet = modelformset_factory(TicketStatus, form=TicketStatusForm, extra=3, can_delete=True)
# TicketTypeFormSet = formset_factory(TicketTypeForm, extra=3, can_delete=True)
TicketTypeFormSet = modelformset_factory(TicketType, form=TicketTypeForm, extra=3, can_delete=True)
class PriorityForm(forms.ModelForm):
class Meta:
model = Priority
fields = ['name', 'comment', ]
class TicketStatusForm(forms.ModelForm):
class Meta:
model = TicketStatus
fields = ['name', ]
class TicketTypeForm(forms.ModelForm):
class Meta:
model = TicketType
fields = ['name', ]
{% load i18n %}
{% load currency_filters %}
<div class="table-header">
<h3>
{% blocktrans %}{{ title }}{% endblocktrans %}
<button type="button" class="btn btn-default pull-right" data-toggle="collapse"
data-target="#{{ id }}">{% trans "Show/Hide" %} {{ count }}
</button>
</h3>
</div>
{% block tag %}
<div id="{{ id }}" class="collapse in">
<form id="{{ id }}-form" method="post" action="." class="form-stacked">
{% csrf_token %}
{% block tag_content %}
{{ tag_formset.management_form }}
{{ tag_formset.non_form_errors }}
<table class="table table-bordered table-striped">
<tbody>
{% for tag_form in tag_formset %}
{% if tag_form.non_field_errors %}
<tr>
<td>
{% for error in tag_form.non_field_errors %}
<span class="error-block errorlist"><i
class="icon-exclamation-sign"></i> {{ error }}</span>
{% endfor %}
</td>
</tr>
{% endif %}
<tr>
{% for field in tag_form %}
<td>
{% include "dashboard/partials/form_field.html" with field=field %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endblock tag_content %}
<div class="form-actions">
<a href="{% url 'support-dashboard:tag-list' %}" class="btn">{% trans "cancel" %}</a>
<div class="pull-right">
<button class="btn btn-primary btn-lg" type="submit"
data-loading-text="{% trans 'Saving...' %}">{% trans "Save" %}</button>
</div>
</div>
</form>
</div>
<hr>
{% endblock tag %}
class TagListView(generic.ListView):
model = Priority
priority_formset = PriorityFormSet
ticket_status_formset = TicketStatusFormSet
ticket_type_formset = TicketTypeFormSet
template_name = 'oscar_support/dashboard/tag_list.html'
def __init__(self, *args, **kwargs):
super(TagListView, self).__init__(*args, **kwargs)
self.formsets = {
'priority_formset': self.priority_formset,
'ticket_status_formset': self.ticket_status_formset,
'ticket_type_formset': self.ticket_type_formset,
}
def get_context_data(self, **kwargs):
ctx = super(TagListView, self).get_context_data(**kwargs)
ctx['type_count'] = TicketType.objects.all().count()
ctx['status_count'] = TicketStatus.objects.all().count()
ctx['priority_count'] = Priority.objects.all().count()
for ctx_name, formset_class in self.formsets.items():
if ctx_name not in ctx:
ctx[ctx_name] = formset_class(
self.request.POST,
# instance=instance
)
return ctx
def process_all_forms(self):
formsets = {}
for ctx_name, formset_class in self.formsets.items():
formsets[ctx_name] = formset_class(
self.request.POST,
# instance=self.object
)
is_valid = all(
[formset.is_valid() for formset in formsets.values()]
)
cross_form_validation_result = self.clean(formsets)
if is_valid and cross_form_validation_result:
return self.forms_valid(formsets)
else:
return self.forms_invalid(formsets)
form_valid = form_invalid = process_all_forms
def clean(self, form, formsets):
return True
def forms_valid(self, form, formsets):
for formset in formsets.values():
formset.save()
return HttpResponseRedirect(self.get_success_url())
def forms_invalid(self, form, formsets):
messages.error(
self.request,
_("Your submitted data was not valid - please "
"correct the errors below")
)
ctx = self.get_context_data(form=form, **formsets)
return self.render_to_response(ctx)
def get_success_url(self):
messages.success(
self.request,
_("Successfully updated {0}.").format(self.object), extra_tags="safe noicon"
)
return reverse("support-dashboard:tag-list")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment