Django формы - как динамически изменять метки полей

Как динамически изменять метки полей в формах в Django В приведенном ниже коде слово labels в функции def clean_passport "закрашивается серым цветом", говоря, что доступ к 'labels' не разрешен

class LegalDocumentUpdateForm(forms.ModelForm):

    # FORM META PARAMETERS
    class Meta:
        model = Legal_Document
        fields = ('document_type', 'name_on_document', 'number_on_document', 'issuing_country', 'issuance_date', 'expiry_date', 'document_location')
        labels = {
            'document_type': _("Document Type*"),
            'name_on_document': _("Name on Document*"),
            'number_on_document': _("Document Number"),
            'issuing_country': _("Issuing Country"),
            'issuance_date': _("Issuance Date"),
            'expiry_date': _("Expiry Date"),
            'document_location': _("Document Location*")
            }



    # SANITIZATION & VALIDATION CHECKS
    def clean(self):
        document_type = self.cleaned_data['document_type']
        if document_type == 'Passport':
            number_on_document = self.cleaned_data['number_on_document']
            issuing_country = self.cleaned_data['issuing_country']
            expiry_date = self.cleaned_data['expiry_date']
            if number_on_document == None:
                raise forms.ValidationError(_("Please enter the Passport Number."))
            if issuing_country == None:
                raise forms.ValidationError(_("Please enter the Passport's Issuing Country."))
            if expiry_date == None:
                raise forms.ValidationError(_("Please enter the Passport's Expiry Date."))
            labels = {
                'name_on_document': _("Passport Full Name*"),
                'number_on_document': _("Passport Number*"),
                'issuing_country': _("Issuing Country*"),
                'expiry_date': _("Passport Expiry Date*"),
            }

Каждое поле имеет атрибут label, который вы можете установить.

E.g.

self.fields['name_on_document'].label = 'Whatever'

Возможно, вы можете использовать это в методе clean. Но я не вижу в этом смысла, так как он не будет отображаться, если в форме нет ошибки.

Вернуться на верх