Multiple formsets on the same page in Django

0

So I have several connected models.
I need to have several formsets which the user can dynamically add (via add more button) all on the same page. So if a person’s has more than 1 job, they’d click the “add more” button and another workingexperience form would appear underneath it. You get the idea.

class Resume(models.Model):
    user = models.OneToOneField(User)
    image = models.ImageField()
    name = models.CharField(max_length=200)


class WorkingExperience(models.Model):
    resume = models.ForeignKey(Resume)
    position = models.CharField(max_length=40)
    company = models.CharField(max_length=70)


class Education(models.Model):
    field_of_study = models.CharField(max_length=40)
    currently_studying = models.BooleanField()
    resume = models.ForeignKey(Resume)


class Skill(models.Model):
    name = models.CharField(max_length=30)
    resume = models.ForeignKey(ZonaCV)


class Language(models.Model):
    name = models.CharField(max_length=20)
    level = models.CharField(choices=language_levels, max_length=3)
    resume = models.ForeignKey(Resume)

I’m using modelforms and inlineformset_factory together with MultiModelForm from django-betterforms.

(just normal modelforms above this)
class ZonaCVMultiForm(MultiModelForm):
    form_classes = OrderedDict((
        ('resume', ResumeForm),
        ('workexp', WorkExpFormset),
        ('eduform', EduFormset),
        ('skillsform', SkillFormset),
        ('langform', LanguageFormset),
        ...
        ))

I’ve used django-dynamic-formset (jquery), because I’ve used it in the past(for one formset).
Now I seem to have a hit a wall (over a week) and can’t seem to go anywhere.
What my template looks like:

{% block content %}
<form action="" method="post">{% csrf_token %}
<div id="resume">{{ form.resume}}</div>
<div id="workexp">{{ form.workexp}}</div>
<div id="eduform">{{ form.eduform}}</div>
<div id="skillsform"> {{ form.skillsform}}</div>
<div id="langform">{{ form.langform}}</div> 
    <input type="submit" value="Save" />
</form>

<script type="text/javascript">
$(function() {
    $('#workexp').formset({
        prefix: 'workexp',
    });
    $('#eduform').formset({
        prefix: 'eduform',
    });
    $('#skillsform').formset({
        prefix: 'skillsform',
    });
    $('#langform').formset({
        prefix: 'langform',
    });
})
</script>
{% endblock %}

I only see a “delete” button appearing on the first formset, but nothing else.

Link: Multiple formsets on the same page in Django
Source: Stack Jquery

Share.

About Author

Leave A Reply