From 98202902d36c0175c952ec9bff9404dba2577cfd Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Tue, 16 Feb 2021 23:49:42 +0100 Subject: [PATCH] =?UTF-8?q?Zm=C4=9Bna=20z=20inlinemodelformsetu=20na=20oby?= =?UTF-8?q?=C4=8Dejn=C3=BD=20formset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Teď už initial funguje správně --- seminar/forms.py | 11 ++++++++--- seminar/templates/seminar/odevzdavatko/detail.html | 8 ++++---- seminar/views/odevzdavatko.py | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/seminar/forms.py b/seminar/forms.py index 71402a58..c64095c6 100644 --- a/seminar/forms.py +++ b/seminar/forms.py @@ -2,6 +2,7 @@ from django import forms from dal import autocomplete from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.models import User +from django.forms import formset_factory from django.forms.models import inlineformset_factory from .models import Skola, Resitel, Osoba, Problem @@ -302,12 +303,16 @@ class NahrajObrazekKTreeNoduForm(forms.ModelForm): fields = ('na_web',) -OhodnoceniReseniFormSet = inlineformset_factory(m.Reseni, m.Hodnoceni, - fields = ('problem', 'body', 'cislo_body'), - extra = 1, +class JednoHodnoceniForm(forms.ModelForm): + class Meta: + model = m.Hodnoceni + fields = ('problem', 'body', 'cislo_body') widgets = { 'problem': autocomplete.ModelSelect2( url='autocomplete_problem_odevzdatelny', # FIXME: Dovolit i starší? ) } + +OhodnoceniReseniFormSet = formset_factory(JednoHodnoceniForm, + extra = 0, ) diff --git a/seminar/templates/seminar/odevzdavatko/detail.html b/seminar/templates/seminar/odevzdavatko/detail.html index 000167a3..dd4979f3 100644 --- a/seminar/templates/seminar/odevzdavatko/detail.html +++ b/seminar/templates/seminar/odevzdavatko/detail.html @@ -38,18 +38,18 @@ function deleteForm(prefix, btn) { // Credit: https://simpleit.rocks/python/django/dynamic-add-form-with-add-button-in-django-modelformset-template/ $(document).ready(function(){ $('#pridat_hodnoceni').click(function() { - var form_idx = $('#id_hodnoceni_set-TOTAL_FORMS').val(); + var form_idx = $('#id_form-TOTAL_FORMS').val(); var new_form = $('#empty_form').html().replace(/__prefix__/g, form_idx); $('#form_set').append(new_form); // Newly created form has not the binding between remove button and remove function // We need to add it manually $('.smazat_hodnoceni').click(function(){ - deleteForm("hodnoceni_set",this); + deleteForm("form",this); }); - $('#id_hodnoceni_set-TOTAL_FORMS').val(parseInt(form_idx) + 1); + $('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1); }); $('.smazat_hodnoceni').click(function(){ - deleteForm("hodnoceni_set",this); + deleteForm("form",this); }); }); diff --git a/seminar/views/odevzdavatko.py b/seminar/views/odevzdavatko.py index 0c182695..e81f119f 100644 --- a/seminar/views/odevzdavatko.py +++ b/seminar/views/odevzdavatko.py @@ -166,6 +166,8 @@ def hodnoceniReseniView(request, pk, *args, **kwargs): # FIXME: Použit initial i tady a nebastlit hodnocení tak nízkoúrovňově # Also: https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#django.forms.ModelForm formset = f.OhodnoceniReseniFormSet(request.POST) + # TODO: Napsat validaci formuláře a formsetu + # TODO: Implementovat větev, kdy formulář validní není. if formset.is_valid(): with transaction.atomic(): # Smažeme všechna dosavadní hodnocení tohoto řešení