官方文件中没有多少 - http://flask-admin.readthedocs.org
我找到了这篇文章 - How to enforce constraints in `flask-admin`? ,但语法不多
我的代码
class NodeConfig(db.Model):
id = db.Column(db.Integer, primary_key=True)
variable = db.Column(db.String(64), nullable=False)
value_or_representation = db.Column(db.String(64), nullable=False)
node_id = db.Column(db.Integer(), db.ForeignKey(Node.id))
node = db.relationship(Node, backref='config')
db.UniqueConstraint('node_id', 'variable', name='uix_1')
def __unicode__(self):
return '%s:%s' % (self.variable, self.value_or_representation)
我也试过自定义视图
class NodeConfigAdmin(sqla.ModelView):
def on_model_delete(self, model):
db.UniqueConstraint('node_id', 'variable', name='uix_1')
def on_model_change(self, form, model, is_created):
db.UniqueConstraint('node_id', 'variable', name='uix_1')
def __init__(self, session):
# Just call parent class with predefined model.
super(NodeConfigAdmin, self).__init__(NodeConfig, session)
但是不会触发约束检查。我能够创建重复的条目。有什么想法吗?