Flask 管理员验证 unique_with 个错误
Flask Admin validate unique_with errors
我有一个带有 unique_with 字段的 flask monoenginge 模型
class RedirectMixin(object):
name = db.StringField(max_length=1000, required=True, help_text="Used internally")
matching_type = db.IntField(
help_text='`Equals` has higher priority. With concurrent `Start with` rules, longest `Url_from` wins',
required=True, choices=MATCHING_TYPES)
add_unmatched_ending = db.BooleanField(
help_text='Example: Starts with /a/ redirect to /b/. This will make /a/c/ redirect to /b/c/',
default=False)
url_from = db.StringField(max_length=1000, required=True, unique_with='matching_type',
help_text='Case insensitive')
url_to = db.StringField(max_length=1000, required=True)
我想知道为什么 flask admin 不验证违反此规范的情况(unique_with 即)当在管理员端填写表单时以及如果 flask 如何进行验证管理员不是为它而建的。提前致谢
原来这种类型的验证需要与数据库交互,而 flask-admin 验证器可能无法提供。我像这样为自己创建了一个自定义验证器:
class Unique(object):
def __init__(self, with_=None, message=None):
self.message = message
self.with_ = with_
def __call__(self, form, field):
query, with_msg = {}, ''
if self.with_:
query[self.with_] = form[self.with_].data
with_msg = 'with "%s"' % self.with_
query[field.name] = field.data
if form._obj:
query['id__ne'] = form._obj.id
matched_entries = form.model_class.objects(**query)
if matched_entries:
if self.message is None:
self.message = field.gettext('Duplicate exists. Value Should be unique ' + with_msg)
raise ValueError(self.message)
is_unique = Unique
然后在我的模型中我这样使用它:
from balut.lib.forms.validators import is_unique
class RedirectMixin(object):
name = db.StringField(max_length=1000, required=True, help_text="Used internally")
matching_type = db.IntField(
help_text='`Equals` has higher priority. With concurrent `Start with` rules, longest `Url_from` wins',
required=True, choices=MATCHING_TYPES)
add_unmatched_ending = db.BooleanField(
help_text='Example: Starts with /a/ redirect to /b/. This will make /a/c/ redirect to /b/c/',
default=False)
url_from = db.StringField(max_length=1000, required=True, unique_with='matching_type',
help_text='Case insensitive')
url_to = db.StringField(max_length=1000, required=True)
form_args = dict(url_from={'validators': [is_unique(with_='matching_type', message=None)]})
我有一个带有 unique_with 字段的 flask monoenginge 模型
class RedirectMixin(object):
name = db.StringField(max_length=1000, required=True, help_text="Used internally")
matching_type = db.IntField(
help_text='`Equals` has higher priority. With concurrent `Start with` rules, longest `Url_from` wins',
required=True, choices=MATCHING_TYPES)
add_unmatched_ending = db.BooleanField(
help_text='Example: Starts with /a/ redirect to /b/. This will make /a/c/ redirect to /b/c/',
default=False)
url_from = db.StringField(max_length=1000, required=True, unique_with='matching_type',
help_text='Case insensitive')
url_to = db.StringField(max_length=1000, required=True)
我想知道为什么 flask admin 不验证违反此规范的情况(unique_with 即)当在管理员端填写表单时以及如果 flask 如何进行验证管理员不是为它而建的。提前致谢
原来这种类型的验证需要与数据库交互,而 flask-admin 验证器可能无法提供。我像这样为自己创建了一个自定义验证器:
class Unique(object):
def __init__(self, with_=None, message=None):
self.message = message
self.with_ = with_
def __call__(self, form, field):
query, with_msg = {}, ''
if self.with_:
query[self.with_] = form[self.with_].data
with_msg = 'with "%s"' % self.with_
query[field.name] = field.data
if form._obj:
query['id__ne'] = form._obj.id
matched_entries = form.model_class.objects(**query)
if matched_entries:
if self.message is None:
self.message = field.gettext('Duplicate exists. Value Should be unique ' + with_msg)
raise ValueError(self.message)
is_unique = Unique
然后在我的模型中我这样使用它:
from balut.lib.forms.validators import is_unique
class RedirectMixin(object):
name = db.StringField(max_length=1000, required=True, help_text="Used internally")
matching_type = db.IntField(
help_text='`Equals` has higher priority. With concurrent `Start with` rules, longest `Url_from` wins',
required=True, choices=MATCHING_TYPES)
add_unmatched_ending = db.BooleanField(
help_text='Example: Starts with /a/ redirect to /b/. This will make /a/c/ redirect to /b/c/',
default=False)
url_from = db.StringField(max_length=1000, required=True, unique_with='matching_type',
help_text='Case insensitive')
url_to = db.StringField(max_length=1000, required=True)
form_args = dict(url_from={'validators': [is_unique(with_='matching_type', message=None)]})