'Django forms - how to dynamically alter field labels
How do I dynamically alter field labels in forms in Django In the below code, the labels word in the def clean_passport function is 'greyed out', saying 'labels' not accessed
Also, 'document_type' is not submitted in the form, it is not included as a form field, but is in the model, and it is assigned a value in the view.... how do I access it, if I need to do customised form validation based on the value of document_type???
Or should I put the logic in the model, and change the field characteristics there? Again, how would I access the value of document_type of a record / object in the model? Can you do that, dynamically change field attributes on a database?
UPDATE
I think I may need to create 4 separate forms as a way around this, unless anyone has a more generalisable / flexible solution
FORM
class LegalDocumentUpdateForm(forms.ModelForm):
# FORM META PARAMETERS
class Meta:
model = Legal_Document
fields = ('name_on_document', 'number_on_document', 'issuing_country', 'issuance_date', 'expiry_date', 'document_location')
labels = {
'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']
# document_type is not submitted in the form, but is in the model, and preset in the view.... how do I access it, if I need to do form validation depending on the value of 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*"),
}
MODEL
# LEGAL DOCUMENT MODEL
class Legal_Document(models.Model):
class LegalDocumentTypes(models.TextChoices):
PASSPORT = 'Passport', _('Passport')
BIRTHCERT = 'Birth Certificate', _('Birth or Adoption Certificate')
CERTOFREG = 'Certificate of Registration', _('Certificate of Registration or Naturalisation')
NIPROOF = 'Proof of N.I.', _('Proof of N.I. Document')
related_user = models.OneToOneField(User, on_delete=models.CASCADE, null=False, default='')
document_id = models.BigAutoField(verbose_name='Document ID', primary_key=True, serialize=False, auto_created=True)
document_type = models.CharField(verbose_name='Document Type', max_length=27, choices=LegalDocumentTypes.choices, blank=False, null=False, default=LegalDocumentTypes.PASSPORT)
name_on_document = models.CharField(verbose_name='Name on Document', max_length=100, blank=False, null=False, default='')
number_on_document = models.CharField(verbose_name='Document Number', max_length=20, blank=True, null=False, default='')
issuing_country = CountryField(verbose_name='Issuing Country', max_length=100, blank=True, null=False, default='')
issuance_date = models.DateField(verbose_name='Issuance Date', blank=True, null=True)
expiry_date = models.DateField(verbose_name='Expiry Date', blank=True, null=True)
document_location = models.CharField(verbose_name='Document Location', max_length=30, blank=False, null=False, default='')
Solution 1:[1]
Each field has a label attribute which you can set.
E.g.
self.fields['name_on_document'].label = 'Whatever'
Maybe you can use that in the clean method. But I don't see the point as it won't be displayed unless there is an error in the form.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vinkomlacic |
