Home > database >  Django ModelForm not displaying Model Verbose Name
Django ModelForm not displaying Model Verbose Name

Time:06-12

I am trying to get my Django ModelForm to label my select dropdown boxes with the Model verbose names. According to the DjangoModel Form documentation "The form field’s label is set to the verbose_name of the model field, with the first character capitalized."

    # model.py
class StLouisCitySale208(models.Model):
    landuse = models.CharField(max_length=254, blank=True, null=True, verbose_name="Land use")
    neighborho = models.CharField(max_length=254, blank=True, null=True, verbose_name="Neighborhood")

# form.py
class StLouisCitySale208Form(ModelForm):
    required_css_class = 'form-group'
    landuse = forms.ModelMultipleChoiceField(widget=forms.SelectMultiple, queryset=StLouisCitySale208.objects.values_list('landuse', flat=True).distinct())
    neighborho =forms.ModelMultipleChoiceField(widget=forms.SelectMultiple, queryset=StLouisCitySale208.objects.values_list('neighborho', flat=True).distinct())
    policedist = forms.ModelMultipleChoiceField(widget=forms.SelectMultiple,queryset=StLouisCitySale208.objects.values_list('policedist', flat=True).distinct())
    class Meta:
        model = StLouisCitySale208
        fields = ['landuse', 'neighborho', 'policedist', 'precinct20','vacantland', 'ward20', 'zip', 'zoning','asmtimprov', 'asmtland', 'asmttotal', 'frontage', 'landarea','numbldgs', 'numunits']

CodePudding user response:

You can do it this way, in the forms.

With label:

landuse = forms.ModelMultipleChoiceField(label='Land use', widget=forms.SelectMultiple, queryset=StLouisCitySale208.objects.values_list('landuse', flat=True).distinct())
  • Related