J
J
Jekson2019-07-24 10:49:13
Django
Jekson, 2019-07-24 10:49:13

How to implement a form with nested fields?

There are 2 related models

class Skill(models.Model):
    """Information about an employee's skills."""

    LEVELS = (
        ('basic', 'Basic'),
        ('intermediate', 'Intermediate'),
        ('advanced', 'Advanced'),
        ('expert', 'Expert'),
    )

    employee = models.ForeignKey(
        Employee, on_delete=models.CASCADE, related_name="employee_skills")
    technology = models.ForeignKey(Technology, on_delete=models.CASCADE)
    year = models.CharField('common year using amount ', max_length=4)
    last_year = models.CharField('Last year of technology using ', max_length=4)
    level = models.CharField("experience level", max_length=64, choices=LEVELS)

class Technology(models.Model):
    """Technologies."""

    name = models.CharField('technology name', max_length=32, unique=True)
    group = models.ForeignKey(Techgroup, on_delete=models.CASCADE, related_name="group")

Their essence is this - each technology has its own value for the level of ownership, years of experience and when it was last used.
I made a form that allows you to edit one technology in one window
class SkillEditForm(forms.ModelForm):
    YEAR_CHOICES = [(r, r) for r in range(1, 11)]
    LAST_YEAR_CHOICES = [(r, r) for r in range(1980, datetime.datetime.now().year + 1)]
    year = forms.CharField(
        widget=forms.Select(choices=YEAR_CHOICES),
    )
    last_year = forms.CharField(widget=forms.Select(choices=LAST_YEAR_CHOICES))

    class Meta:

        model = Skill
        fields = ['technology', 'level', 'last_year', 'year']

There are several dozen technologies in the database. Now I want to implement the ability to edit all technologies at once in one window, so that the user does not have to press the add technology button 10 times. And here I stalled, how to implement it. I will be grateful for advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2019-07-24
@Lepilov

It's in the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question