Browse Source

Změna z inlinemodelformsetu na obyčejný formset

Teď už initial funguje správně
export_seznamu_prednasek
Pavel "LEdoian" Turinsky 4 years ago
parent
commit
98202902d3
  1. 11
      seminar/forms.py
  2. 8
      seminar/templates/seminar/odevzdavatko/detail.html
  3. 2
      seminar/views/odevzdavatko.py

11
seminar/forms.py

@ -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,
) )

8
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/ // 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>

2
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ě # 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í

Loading…
Cancel
Save