I got 3 models:
class Client(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=40)
class Phone(models.Model):
number = models.CharField(max_length=10)
client = models.ForeignKey(Client)
class ClientEmail(models.Model):
client = models.ForeignKey(Client)
address = models.EmailField(verbose_name='Email')
one form and two inline formsets:
class ClientForm(ModelForm):
class Meta:
model = Client
class PhoneFormSet(InlineFormSet):
model = Phone
extra = 1
class EmailFormSet(InlineFormSet):
model = ClientEmail
extra = 1
view:
class ClientCreateView(LoginRequiredMixin, CreateWithInlinesView):
model = Client
inlines = [PhoneFormSet, EmailFormSet,]
and working template:
{% extends 'base.html' %}
{% block extra_head_script %}
<script src="{{ STATIC_URL }}js/jquery.formset.js"></script>
{% endblock %}
{% block content %}
<form action="." method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
{% for formset in inlines %}
<div id="{{ formset.prefix }}">
{% for subform in formset.forms %}
<table>
{{ subform.as_table }}
</table>
{% endfor %}
{{ formset.management_form }}
</div>
{% endfor %}
<input type="submit" value="Add client" class="submit"/>
</form>
{% endblock %}
I just started using ClassBasedViews and cant figure out how to use django-dynamic-formset js with django-extra-views in my template.
Oh my head ... I figured it out:
If you see any errors please point them to me. Also appreciate any better ideas.
For those who want to use this - Yes it should work with any number of formsets