I am creating a custom UserViewCreateForm (using wtforms) in my flask-admin as follows:-
from app.vendors.models import Vendor
class UserViewCreateForm(form.Form):
username = fields.TextField('Username')
first_name = fields.TextField('First Name')
last_name = fields.TextField('Last Name')
email = fields.TextField('Email')
contact_number = fields.TextField('Contact Number')
password = fields.PasswordField('Password')
is_admin = fields.BooleanField('Is Admin')
is_active = fields.BooleanField('Is Active')
is_verified = fields.BooleanField('Is Verified')
vendor = fields.SelectField('Vendor', coerce=int)
class UserView(ModelView):
form_overrides = dict(title=SelectField)
form_args = dict(
# Pass the choices to the `SelectField`
title=dict(
choices=TITLE_TYPE
))
def __init__(self, session, **kwargs):
super(UserView, self).__init__(User, session, **kwargs)
def is_accessible(self):
return login.current_user.is_authenticated()
def create_form(self):
form = UserViewCreateForm()
form.vendor.choices = [(0, '')] + [(v.id, v.name) for v in Vendor.query.all()]
return form
The vendor select field is giving me a Not a valid choice validation error.
What am I doing wrongly?
Possibly because you have no choices?
CA_STATES = [
('', 'Province'),
('AB', 'Alberta'),
('BC', 'British Columbia'),
('MB', 'Manitoba'),
('NB', 'New Brunswick'),
('NL', 'Newfoundland and Labrador'),
('NS', 'Nova Scotia'),
('ON', 'Ontario'),
('PE', 'Prince Edward Island'),
('QC', 'Quebec'),
('SK', 'Saskatchewan'),
('NT', 'Northwest Territories'),
('NU', 'Nunavut'),
('YT', 'Yukon'),
]
state_ca = SelectField('Province', choices=CA_STATES)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With