Změna z inlinemodelformsetu na obyčejný formset

Teď už initial funguje správně
This commit is contained in:
Pavel "LEdoian" Turinsky 2021-02-16 23:49:42 +01:00
parent 9d79c052fe
commit 98202902d3
3 changed files with 14 additions and 7 deletions

View file

@ -2,6 +2,7 @@ from django import forms
from dal import autocomplete from dal import autocomplete
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.forms import formset_factory
from django.forms.models import inlineformset_factory from django.forms.models import inlineformset_factory
from .models import Skola, Resitel, Osoba, Problem from .models import Skola, Resitel, Osoba, Problem
@ -302,12 +303,16 @@ class NahrajObrazekKTreeNoduForm(forms.ModelForm):
fields = ('na_web',) fields = ('na_web',)
OhodnoceniReseniFormSet = inlineformset_factory(m.Reseni, m.Hodnoceni, class JednoHodnoceniForm(forms.ModelForm):
fields = ('problem', 'body', 'cislo_body'), class Meta:
extra = 1, model = m.Hodnoceni
fields = ('problem', 'body', 'cislo_body')
widgets = { widgets = {
'problem': autocomplete.ModelSelect2( 'problem': autocomplete.ModelSelect2(
url='autocomplete_problem_odevzdatelny', # FIXME: Dovolit i starší? url='autocomplete_problem_odevzdatelny', # FIXME: Dovolit i starší?
) )
} }
OhodnoceniReseniFormSet = formset_factory(JednoHodnoceniForm,
extra = 0,
) )

View file

@ -38,18 +38,18 @@ function deleteForm(prefix, btn) {
// Credit: https://simpleit.rocks/python/django/dynamic-add-form-with-add-button-in-django-modelformset-template/ // Credit: https://simpleit.rocks/python/django/dynamic-add-form-with-add-button-in-django-modelformset-template/
$(document).ready(function(){ $(document).ready(function(){
$('#pridat_hodnoceni').click(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); var new_form = $('#empty_form').html().replace(/__prefix__/g, form_idx);
$('#form_set').append(new_form); $('#form_set').append(new_form);
// Newly created form has not the binding between remove button and remove function // Newly created form has not the binding between remove button and remove function
// We need to add it manually // We need to add it manually
$('.smazat_hodnoceni').click(function(){ $('.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(){ $('.smazat_hodnoceni').click(function(){
deleteForm("hodnoceni_set",this); deleteForm("form",this);
}); });
}); });
</script> </script>

View file

@ -166,6 +166,8 @@ def hodnoceniReseniView(request, pk, *args, **kwargs):
# FIXME: Použit initial i tady a nebastlit hodnocení tak nízkoúrovňově # 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 # Also: https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#django.forms.ModelForm
formset = f.OhodnoceniReseniFormSet(request.POST) formset = f.OhodnoceniReseniFormSet(request.POST)
# TODO: Napsat validaci formuláře a formsetu
# TODO: Implementovat větev, kdy formulář validní není.
if formset.is_valid(): if formset.is_valid():
with transaction.atomic(): with transaction.atomic():
# Smažeme všechna dosavadní hodnocení tohoto řešení # Smažeme všechna dosavadní hodnocení tohoto řešení