I am pretty new to python and sqlalchemy and would like to ask for an advice.
What would be the best way to force uppercase values in a SqlAlchemy field when the record is inserted/updated? Should I use events for that? Here I am using flask-sqlalchemy version, but I believe it is something similar to SQLAlchemy.
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Item(db.Model):
# I need to ensure the code column converts values to uppercase automatically
code = db.Column(db.String(30), primary_key=True)
name = db.Column(db.String(250), nullable=False)
...
Thanks for the advice
Have a look at events
YOu can easily add a listener for a save/update and just uppercase the string:
def uppercase_code(target, value, oldvalue, initiator):
return value.upper()
# setup listener on Item.code attribute, instructing
# it to use the return value
listen(Item.code, 'set', uppercase_code, retval=True)
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