Změna z inlinemodelformsetu na obyčejný formset
Teď už initial funguje správně
This commit is contained in:
parent
9d79c052fe
commit
98202902d3
3 changed files with 14 additions and 7 deletions
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
|
@ -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í
|
||||
|
|
Loading…
Reference in a new issue