In most cases not defining a constructor in your model class gives you the correct behavior.
Flask-SQLAlchemy's base model class (which is also SQLAlchemy's declarative base class) defines a constructor that just takes **kwargs
and stores all the arguments given, so it isn't really necessary to define a constructor.
If you do need to define a constructor to do some model specific initialization, then do so as follows:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
# do custom initialization here
By letting the base class handle the **kwargs
you free yourself from the complexity of initializing the fields of the model.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…