Answer the question
In order to leave comments, you need to log in
Is it possible to combine a form field in a formset?
Good day. It is necessary to make a schedule of classes for an educational institution. Tell me how best to make a form for adding a schedule, so that it looks like this:
So far, I could only do this, through inlineformset_factory:
So, is it possible to somehow combine the "Date" fields of the forms in the formset, or, let's say, do field "Date" in the first form is visible, and in the other three - invisible, and assign them the value of the field of the first form?
Or in general it is possible to do without formset?
views.py:
import mimetypes
import os
from django.shortcuts import render
from .forms import ScheduleForm
from django.http import HttpResponseRedirect, HttpResponse
from django.forms import inlineformset_factory
from .models import Schedule
from groups.models import Group
def addschedule(request, gn=''):
gnum = Group.objects.get(pk=gn)
model_formset = inlineformset_factory(parent_model=Group, model=Schedule, form=ScheduleForm, exclude=[], extra=24,
can_delete=False)
if request.POST:
formset = model_formset(request.POST, request.FILES, instance=gnum)
if formset.is_valid():
estates_objects = formset.save(commit=False)
for form in formset:
form.save()
return render(request, 'main/mainpage.html', locals())
else:
formset = model_formset(queryset=Schedule.objects.all())
return render(request, 'schedule/schedule.html', locals())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question