My code:
# db.py
class NewModel(Model):
user = sa.Column(sa.String(256), nullable = False, default = 'User')
uploaded = sa.Column(sa.DateTime, nullable = False, default = datetime.utcnow)
def __init__(self, **kwargs):
kwargs['user'] = session['username']
super().__init__(**kwargs)
def save(self):
db.session.add(self)
db.session.commit()
db = SQLAlchemy(model_class = NewModel)
# upload_file.py
class UploadFile(db.Model):
id = db.Column(db.Integer, primary_key = True)
filename = db.Column(db.String(256), nullable = False)
path = db.Column(db.String(256), nullable = False, unique = True)
def __init__(self, file):
self.file = file
kwargs = {
'filename': file.filename,
'path': secure_filename(file.filename)
}
super().__init__(**kwargs)
def valid(self):
return self.file is not None and \
self.file.filename.strip() != ''
When I try to make an instance of UploadFile it works, but it doesn't call NewModel's constructor. Why?
I've found this issue, but it didn't solve my problem.
And I tried to use an orm.reconstructor decorator but it didn't work either.