I am using sqlalchemy and use UUID for the primary keys in my database (postgres)
Here is my class:
class EntityType(db.Model):
__tablename__ = 'entity_type'
id = db.Column(UUID(as_uuid=True), primary_key=True, server_default=db.text('gen_random_uuid()'))
name = db.Column(db.Text, nullable=False)
In the form to add an entity, the user has to choose an entity type, I have the following
class EntityAddForm(FlaskForm):
url = StringField('URL', validators=[DataRequired(),URL(require_tld=True)])
entity_type = SelectField('Entity type')
description = TextAreaField('Description')
submit = SubmitField('Add')
def validate_entity_type(self, entity_type):
entity_type = EntityType.query.filter_by(id=entity_type.data).first()
if entity_type is None:
raise ValidationError('Please select a different entity type.')
To load the form, I have the following code to populate the selectField with rows of the table.
form = EntityAddForm()
form.entity_type.choices = [(et.id, et.name) for et in EntityType.query.order_by('name')]
The issue is that the if form.validate_on_submit(): does not return True on the submit.
It seems that WTForms needs a string/unicode/int using coerce for the id of the selectField
Is there a way to keep using the UUID type instead of switching to integer to get the selectField working?
I tried with QuerySelectEntity or a custom UUID type but could not succeed.
Thank you