2

I need to add the empty choice value in existing valuelistqueryset for below choicefield in my form in Django similat to modelchoicefield("-------------"). Can anyone help me with this?

hardness = forms.ModelChoiceField(queryset=Description.objects.filter(variable="hardness", paradigm='206209').values_list('dvalue', flat=True).distinct().order_by('dvalue'))

@jghyllebert Thanks for your reply. But in this case I need to convert modelchoicefield to choicefield. And I need the solution for choicefield. Sorry if i was not clear about it. I tried empty_label and empty_value in choice field but the form goes in error.

rimplan = forms.ChoiceField(
    required=False, 
    empty_value = '---',
    choices=Description.objects.filter(
        variable="rimplan", 
        paradigm='206209').values_list(
            'dvalue', 
            flat=True).distinct().order_by('dvalue')) 

Thus, I have implemented this solution which work perfectly fine.

EXTSURCOLOR_DESCRIPTIONS = Description.objects.filter(
    variable="exteriorsurfacecolor", 
    paradigm='206209') 

EXTSURCOLOR_CHOICES = [('', '---')] 

EXTSURCOLOR_CHOICES.extend(
    EXTSURCOLOR_DESCRIPTIONS.values_list(
        "dvalue", 
        "dvalue").distinct().order_by('dvalue')) 

exteriorsurfacecolor = forms.ChoiceField(choices = EXTSURCOLOR_CHOICES) 

I just wanted to know if there is any better solution out there.

Bill Armstrong
  • 1,615
  • 3
  • 23
  • 47
  • Is there something about the empty value that will be displayed if you set `required=False` that isn't working for you? You can't literally insert it into the queryset, but it'll add it to the choices offered for the field. – Peter DeGlopper Jun 26 '13 at 21:47

1 Answers1

1

There is an option empty_label which you can use to set a custom value for the empty choice option.

hardness = forms.ModelChoiceField(
    queryset=Description.objects.filter(variable="hardness", paradigm='206209').values_list('dvalue', flat=True).distinct().order_by('dvalue'),
    empty_label='Not applicable', #for instance
    required=False
)

Following the docs:

Note that if a ModelChoiceField is required and has a default initial value, no empty choice is created (regardless of the value of empty_label).

J. Ghyllebert
  • 2,009
  • 1
  • 29
  • 35
  • @jghyllebert Thanks for your reply. But in this case I need to convert modelchoicefield to choicefield. And I need the solution for choicefield. Sorry if i was not clear about it. I tried empty_label and empty_value in choice field but the form goes in error. rimplan = forms.ChoiceField(required=False, empty_value = '---', choices=Description.objects.filter(variable="rimplan", paradigm='206209').values_list('dvalue', flat=True).distinct().order_by('dvalue')) – user1908622 Jun 27 '13 at 13:47
  • Thus, I have implemented this solution which work perfectly fine. EXTSURCOLOR_DESCRIPTIONS = Description.objects.filter(variable="exteriorsurfacecolor", paradigm='206209') EXTSURCOLOR_CHOICES = [('', '---')] EXTSURCOLOR_CHOICES.extend(EXTSURCOLOR_DESCRIPTIONS.values_list("dvalue", "dvalue").distinct().order_by('dvalue')) exteriorsurfacecolor = forms.ChoiceField(choices = EXTSURCOLOR_CHOICES) I just wanted to know if there is any better solution out there. – user1908622 Jun 27 '13 at 13:50
  • could post this code in your question? It's quite hard to read now. – J. Ghyllebert Jun 27 '13 at 13:54