Serenity
Serenity

Reputation: 4054

show forms for model who can have multiple instance

I am creating a simple project which is about creating a resume by user. In resume, a user can have multiple experience, educational background and etc. That is why I have created the following table where experience, educational background, skills are foreignkey to the resume table.

class Resume(models.Model):
    applicant = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100, blank=False, null=False, help_text="Full Name")
    slug = models.SlugField(max_length=50, unique=True)
    designation = models.CharField(max_length=200, blank=True, null=True)
    city = models.CharField(max_length=100, blank=True, null=True)

    def __str__(self):
        return self.name

class Education(models.Model):
    resume = models.ForeignKey(Resume, related_name='education')
    name = models.CharField(max_length=100, blank=False, null=False, help_text="Name of an institution")
    course = models.CharField(max_length=200, blank=False, null=False, help_text="Name of a course")
    description = models.CharField(max_length=400, blank=True, null=True)
    start_date = models.DateField()
    end_date = models.DateField()

class Experience(models.Model):
    resume = models.ForeignKey(Resume, related_name='experience')
    designation = models.CharField(max_length=100, blank=True, null=True)
    company = models.CharField(max_length=100, blank=True, null=True)
    description=models.CharField(max_length=400, blank=True, null=True)
    start_date = models.DateField()
    end_date = models.DateField()

class Skill(models.Model):
    resume=models.ForeignKey(Resume, related_name="skills")
    name = models.CharField(max_length=100, blank=True, null=True, help_text="Name of the skill")

    class Meta:
        verbose_name='Skill'
        verbose_name_plural='Skills'

    def __str__(self):
        return self.name

Now for such situation, do I have to create a ResumeForm, EducationForm, ExperienceForm etc and create an Education, Experience and Skill formset or I have to do something else. I do not have clear idea on how to move forward now for developing form with such relation where Education, Skill can have multiple instance. Can anyone guide me, please?

Upvotes: 0

Views: 22

Answers (1)

Mauricio Cortazar
Mauricio Cortazar

Reputation: 4213

Well the question is unclear but following with your idea you have 2 options:

First you can have existing values in Education, Experience, Skill. Then in the view you have a checkbox to add education, experience, skill.

Second you can add education, experience, skill creating a modelForm for each one and then passing the resume, It is not necessary use formset here

Upvotes: 1

Related Questions