I just want to add an initial value to one field when it gets populated, this is my view
def add_program(request):
module = Module.objects.all()
last_item = Programme.objects.filter(module_id__in=module).last()
if last_item:
day = last_item.day + 1
else:
day = 1
initial_data = {
'day': day
}
if request.method == 'POST':
form = ProgramAddForm(request.POST or None, request.FILES or None, initial=initial_data)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
return redirect('program_add')
else:
form = ProgramAddForm()
return render(request, 'client_apps/program/form.html', {'form': form})
and form
class ProgramAddForm(forms.ModelForm):
class Meta:
model = Programme
fields = ['module', 'day', .........]
even though initial value is passed nothing is visible in day field when form populates, is there any alternative method?