DJANGO 模型表单初始值

DJANGO MODEL FORM INITIAL VALUE

提问人:ENTHRONE KINGSLEY 提问时间:9/17/2023 更新时间:9/17/2023 访问量:22

问:

请帮助我为我的模型表单设置初始值。

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'

字段为 12 个,所有 12 个字段均不适用于用户。我希望用户只填写适用于他们的字段,并将其他字段作为 0 初始值提交。 我想知道覆盖 的 init 函数是否会解决问题。

姜戈 默认值 基于 django 类的视图 模型形式

评论

0赞 markwalker_ 9/17/2023
如果您知道创建表单时需要哪些字段,那么是的,这将解决问题。

答: 暂无答案