Как добавить несколько форм на одну страницу с помощью django formset

Используя Django formset, вы можете добавить сразу несколько форм на одну страницу. Однако, возникли две проблемы. Гугление в течение нескольких дней не решает их.

[forms.py]

from django import forms
from django.forms import modelformset_factory, ModelForm, DateTimeInput, Select
from .models import Supporting

SupportingModelFormset = modelformset_factory(
    Supporting,
    fields=('date', 'student', 'kinds', 'post_hour', 'teacher', 'comment', 'technician'),
    extra=1,
    widgets={'date': forms.DateTimeInput(format=('%Y-%m-%d %H:%M'), attrs={'name': 'date', 'id': 'date', 'autocomplete': 'off', 'class': 'form-control col-sm-12 date'}),
         'student': forms.Select(attrs={'id': 'student', 'name': 'student', 'class': 'chosen'}),
         'kinds': forms.Select(attrs={'id': 'kinds', 'name': 'kinds'),
         'post_hour': forms.TextInput(attrs={'type': 'text', 'class': 'form-control col-sm-3', 'name': 'post_hour', 'placeholder': 'hr'}),
         'cteacherrc': forms.TextInput(attrs={'type': 'text', 'class': 'form-control col-sm-12', 'name': 'teacher'}),
         'comment': forms.TextInput(attrs={'class': 'form-control position-relative', 'name': 'comment', 'rows': '4'})
         }
)

[views.py]

def add_supporting(request):
    if request.method == 'GET':
        formset = SupportingModelFormset(queryset=Supporting.objects.none())
    elif request.method == 'POST':
        formset = SupportingModelFormset(request.POST)
        if formset.is_valid():
            for form in formset:
                form.save()
            return redirect('supporting_list')

return render(request, 'supporting/add.html', {'formset': formset})

[support/add.html]

  1. I want to set the values ​​of the existing row to appear as default in the new row when the '+' button is clicked. Do I need to modify javascript ?

  2. The date field applies the datetimepicker. If I try to add a date/time to the 'date' field by adding a new row through the '+' button, the date/time of 'date' in the existing (first row) row moves together. Whenever a new row is added, the 'date' field has a unique name. It has the following format and is incremented by 1. How to write a regular expression that takes a number as input?

[add.html]

$( ".date" ).datetimepicker({
        format: 'Y-m-d H:i'
    });

<input type="text" name="form-0-date" id="date" autocomplete="off" class="form-control col-sm-12 date">
<input type="text" name="form-1-date" id="id_form-1-date" autocomplete="off" class="form-control col-sm-12 date">
Вернуться на верх