Empty Label for a field with ModelChoiceField

0

Hello, I have a form (in django 1.8) with a field that uses a ModelChoicefield

class MyForm(forms.Form):
    ...
    campoA = forms.ModelChoiceField(
        queryset=ModeloB.objects.values_list('campoA__name', flat=True).distinct(),
        label=_('Campo A'),
        required=False)
    ...

This is shown as follows:

I would like 2 things:

  • That the None option does not appear
  • That you can change the '------' option to another such as 'Select City'
  • Edited

    I solved part 2 with the definition of empty_label, could you help me with the first doubt? so that the None option is not visible?

        
    asked by Diana Carolina Hernandez 18.10.2017 в 22:52
    source

    1 answer

    1

    Check that in the database you probably have a None in the A field of the ModelB. If you do not want to delete that record you can always exlude it:

    queryset=ModeloB.objects.values_list('campoA__name', flat=True).exclude(campoA__name=None).distinct()
    
        
    answered by 19.10.2017 в 12:27