Flask-Admin SQLAlchemy: How to set an attribute value according to another with a python function?
Flask-Admin SQLAlchemy: How to set an attribute value according to another with a python function?
I'm using Python 3.6.5, Flask 1.0.2. I want to define an attribute of a db Model according to another with a python function, like this:
def define_category(age):
if age < 12:
return 'child'
elif age < 20:
return 'teen'
else:
return 'adult'
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(100))
last_name = db.Column(db.String(100))
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
age = db.Column(db.Integer)
category = define_category(age)
def __str__(self):
return self.username
I've tried with hybrid attributes and it doesn't appear to work with different types of data.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.