DJANGO MODEL FORM INITIAL VALUE

28 Views Asked by At

Please I need help with setting initial value for my model form.

from django import forms
from multipage_form.forms import MultipageForm, ChildForm
import calculation
from .models import VinanPetLtd



class ReportForm(MultipageForm):
    model = VinanPetLtd
    starting_form = "Stage1Form"

    class Stage1Form(ChildForm):
        display_name = "Transaction History"
        required_fields = ["entry_Date", "branch", "product"]
        next_form_class = "Stage2Form"


        class Meta:
            fields = ["entry_Date", "branch", "product"]
            widgets = {
                'entry_Date': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
            }
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)


    class Stage2Form(ChildForm):
        display_name = "Tank History"
        required_fields = "__all__"
        next_form_class = "Stage3Form"

        class Meta:
            fields = [
                "tank_1_Opening",
                "tank_1_Closing",
                ....
]
widgets = {
                'tank_1_Difference': calculation.FormulaInput('tank_1_Opening-tank_1_Closing'),
                'tank_1a_Difference': calculation.FormulaInput('tank_1a_Opening-tank_1a_Closing'),
                'tank_1b_Difference': calculation.FormulaInput('tank_1b_Opening-tank_1b_Closing'),
...]


viwes.py
from django.urls import reverse_lazy
from django.views.generic import TemplateView
from multipage_form.views import MultipageFormView
from .forms import JobApplicationForm


class JobApplicationView(MultipageFormView):
    template_name = 'mpf/form_page.html'
    form_class = JobApplicationForm
    success_url = reverse_lazy("mpf:thank_you")


class JobApplicationThankYouView(TemplateView):
    template_name = 'mpf/thank_you.html'

The fields are 12 and all 12 fields are not applicable to users. I want users to only fill fields applicable to them and submit with other fields as 0 initial value. I was wondering if overwriting the init function of would solve the problem.

0

There are 0 best solutions below