Django ModelForm with extra fields that are not in the model

后端 未结 7 410
甜味超标
甜味超标 2021-01-31 02:00

I have done a ModelForm adding some extra fields that are not in the model. I use these fields for some calcualtions when saving the form.

The extra fie

7条回答
  •  离开以前
    2021-01-31 02:32

    In Django 2 you can just add the fields as it was a normal form

    class CreateCompanyForm(forms.ModelForm):
    
        password_confirmation = forms.CharField(
            label=translate('Password confirmation'),
            max_length=70,
            widget=forms.PasswordInput(),
            required=True,
        )
        company_name = forms.CharField(
            label="Nombre de la Compañía",
            max_length=90,
            widget=forms.TextInput(),
            required=True,
        )
    
        class Meta:
            model = AppUser
            fields = (
                "email",
                "first_name",
                "last_name",
                "password",
            )
    

提交回复
热议问题