kvesteri / wtforms-alchemy

Tools for creating wtforms from sqlalchemy models
Other
245 stars 59 forks source link

CountryField empty option when CountryType is nullable #141

Open TrilceAC opened 5 years ago

TrilceAC commented 5 years ago

There might be cases where a CountryField should allow to select an empty/blank option: If the model has columns of CountryType which are nullable, the related CountryField should allow the empty option.

At the moment I have tried to fake the form field choices when the form is created, but with no luck because at some point there are failed coercions to Country that fails. This doesn't work:

class PersonForm(ModelForm):
    # ...
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.country.choices = [(None, '')] + self.nacionalidad.choices()

How should be handled the fact that the country field might be optional?