Skip to content

Instantly share code, notes, and snippets.

@gpennington
Created December 7, 2011 23:00
Show Gist options
  • Save gpennington/1445173 to your computer and use it in GitHub Desktop.
Save gpennington/1445173 to your computer and use it in GitHub Desktop.
Multiple Inline Formsets example for Eddie
<form method="post" action=".">
{{ sunday_formset.management_form }}
{{ monday_formset.management_form }}
{{ tuesday_formset.management_form }}
{% csrf_token %}
<p>sunday form</p>
{% for form in sunday_formset %}
<p>{{ form }}</p>
{% endfor %}
<p>mon form</p>
{% for form in monday_formset %}
<p>{{ form }}</p>
{% endfor %}
<p>tues form</p>
{% for form in tuesday_formset %}
<p>{{ form }}</p>
{% endfor %}
<input type='submit'>
</form>
def home(request):
sunday_dates = Date.objects.get(pk=Date.objects.get(day_of_week="Sunday").pk)
monday_dates = Date.objects.get(pk=Date.objects.get(day_of_week="Monday").pk)
tuesday_dates = Date.objects.get(pk=Date.objects.get(day_of_week="Tuesday").pk)
if request.method == 'POST':
sunday_formset = DateFormSet(request.POST, instance=sunday_dates, prefix='sun')
monday_formset = DateFormSet(request.POST, instance=monday_dates, prefix='mon')
tuesday_formset = DateFormSet(request.POST, instance=tuesday_dates, prefix='tues')
if sunday_formset.is_valid():
sunday_formset.save()
if monday_formset.is_valid():
monday_formset.save()
if tuesday_formset.is_valid():
tuesday_formset.save()
else:
sunday_formset = DateFormSet(instance=sunday_dates, prefix='sun')
monday_formset = DateFormSet(instance=monday_dates, prefix='mon')
tuesday_formset = DateFormSet(instance=tuesday_dates, prefix='tues')
return render(request, "sample/home.html", {'sunday_formset':sunday_formset, 'monday_formset':monday_formset, 'tuesday_formset':tuesday_formset})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment