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.