我想从ID生成一些唯一的URL,目的是追加到某些端点的末尾。结果将类似于...... / 32dwr4。我想基于主键id在实例化中将这些短URL插入到数据库中。
我不知道在模型中是否有某种“冲洗”操作:
class Storm(db.Model):
id = db.Column(db.Integer, primary_key=True)
_url = db.Column(db.String(200), index=True)
## Relationships
@hybrid_property
def url(self):
return self._url
@url.setter
def _set_url(self, plaintext):
self._url= base64.b64encode(plaintext)
def __init__(self, name, question, *):
self.name = name
self.question = question
self.url = self.id # <<------ is it possible to pass its id on the fly, convert it through the setter and store it?
如果不可能,您建议使用哪种方法?