Как сохранить несколько объектов из одного представления в одну модель в Django?

У меня есть шаблон, в котором есть столько полей ввода, сколько целей у пользователя. Я хочу сохранить все значения ввода в одной модели, но как разные объекты. Поэтому я хочу сделать функцию массового сохранения, но метод, который я использую сейчас, сохраняет только последнее введенное значение.

models.py

class IndividualGoal(models.Model):

    def __str__(self):
        return str(self.user)

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_goal")
    leader = models.ForeignKey(User, on_delete=models.CASCADE, related_name="leader_added_individual_goal")
    datetime = models.DateTimeField(auto_now_add=True, auto_now=False, blank=True, null=True)
    goal = models.CharField(max_length=500, null=True, blank=True)
    deadline = models.DateField(auto_now_add=False, auto_now=False, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True, null=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)
    period = models.ForeignKey('Period', on_delete=models.CASCADE, blank=True, null=True)

class IndividualEvaluation(models.Model):

    def __str__(self):
        return str(self.user)

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_evaluation")
    leader = models.ForeignKey(User, on_delete=models.CASCADE, related_name="leader_added_individual_evaluation")
    datetime = models.DateTimeField(auto_now_add=True, auto_now=False, blank=True, null=True)
    goal = models.ForeignKey(IndividualGoal, on_delete=models.CASCADE, blank=True, null=True)
    evaluation = models.CharField(max_length=500, null=True, blank=True)
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True, null=True)
    period = models.ForeignKey('Period', on_delete=models.CASCADE, blank=True, null=True)

views.py

def individual_evaluation(request, company_uuid, user_id, period_id):

    individual_goals = Profile.objects.raw("SELECT *, target_individualgoal.id AS goal_id FROM stressz_profile LEFT JOIN stressz_company ON stressz_profile.company_id = stressz_company.id LEFT JOIN target_individualgoal ON target_individualgoal.user_id = stressz_profile.user_id WHERE stressz_company.company_uuid = %s AND stressz_profile.user_id = %s AND target_individualgoal.period_id = %s GROUP BY target_individualgoal.id", [company_uuid, user_id, period_id])

    form = IndividualEvaluationFrom(request.POST)

    if request.method == 'POST':
        if form.is_valid():
            form.instance.user_id = user_id
            form.instance.leader_id = request.user.id
            form.instance.period_id = period_id
            form.save()

    context = {
        'individual_goals': individual_goals,
    }
    
    return render(request, 'target/evaluation/individual.html', context)

forms.py

class IndividualEvaluationFrom(forms.ModelForm):
    
    class Meta:
        model = IndividualEvaluation
        fields = ['goal', 'evaluation'] 

html

<form method="post">
     {% csrf_token %}
           {% for i in individual_goals %} 
                <div class="mb-3">
                    <label for="goal" class="form-label">{{ i.goal }}</label>
                    <input type="text" class="form-control" id="evaluation" name="evaluation">
                    <input type="hidden" class="form-control" value="{{ i.goal_id }}" id="goal" name="goal">
                    </div>
                {% endfor %}
                <button type="submit" class="btn btn-primary">Mentés</button>
        </form>
Вернуться на верх