Hello,
I'm defining a ChoiceField based on a model's datas.
field = forms.ChoiceField(choices=[[r.id, r.name] for r in Model.objects.all()])
However I'd like to prepend my options with an empty one to select "no" objects. But I can't find a nice way to prepend that.
All my tests like :
field = forms.ChoiceField(choices=[[0, '----------']].extend([[r.id, r.name] for r in Model.objects.all()]))
Returns me a "NoneType object not iterable" error. The only way I've found until now is the following :
def append_empty(choices):
ret = [[0, '----------']]
for c in choices:
ret.append(c)
return ret
And when I define my field :
forms.ChoiceField(choices=append_empty([[r.id, r.name] for r in
Restaurant.objects.all()]), required=False)
However I'd like to keep my code clean and not have that kind of horrors. Would you have an idea for me ? :p Thanks by advance.