python - SQLAlchemy custom field with default value for file uploads in Flask -
i'm trying create custom field in sqlalchemy managing uploaded files in flask application. i'm trying achieve similar django's filefield.
so want able is, given model:
class testmodel(base): __tablename__ = "testmodel" id = column(integer(), primary_key=true) profile_photo = column(filefield(upload_set, 100))
i want able like:
m = testmodel() m.profile_photo.save(request.files["profile_photo"]) models.db.session.add(m) models.db.session.commit()
what have based on built in string type , works this:
class filefield(types.typedecorator): impl = string def __init__(self, upload_set, *args, **kwargs): super(filefield, self).__init__(*args, **kwargs) self.upload_set = upload_set def process_bind_param(self, value, dialect): if value not none: return value.filename return value def process_result_value(self, value, dialect): return filenamestring(self.upload_set, value) class filenamestring(object): def __init__(self, upload_set, filename): self.upload_set = upload_set self.filename = filename def save(self, req_file): self.filename = self.upload_set.save(req_file) return self.filename @property def url(self): return self.upload_set.url(self.filename)
this works when model exists in database not when model created scratch profile_photo
attribute none
. ideally want default value of type blank filefield
, although haven't figured out like. problem can't figure out how create default value attribute without having in constructor of class uses filefield
.
so, idea how create default value profile_photo
attribute?
Comments
Post a Comment