簡體   English   中英

Flask 自定義驗證錯誤 - 類型錯誤:不支持的類型“<class 'wtforms.fields.core.UnboundField'> ”

[英]Flask Custom Validation Error - TypeError: Unsupported type “<class 'wtforms.fields.core.UnboundField'>”

我正在嘗試在 Flask WTF 中添加自定義驗證方法,但它一直失敗。

所以我的目標是在驗證表單時檢查 dynamodb 表中是否存在該值。

不知道我錯過了什么

def pipeline_exists(self, pipeline_name):
    ses = boto3.session.Session()
    try:
        dynamodb = ses.resource('dynamodb')
    except Exception as exception:
        message = 'Error on DynamoDB client'
        logging.error("Dynamodb client session failed: %d %s", exception)
        msg = " Dynamodb client session failed: " + str(exception)

        raise exception
    table_name = 'my_tbl'
    table = dynamodb.Table(table_name)

    response = table.query(
        KeyConditionExpression=Key('pipeline_name').eq(pipeline_name)
    )

    print(response)
    if response['count'] > 0:
        raise validators.ValidationError('Pipeline ' + pipeline_name.data + ' already exists')
    else:
        True

而形式是

class AddPipeline(Form):
    pipeline_name = StringField('Pipeline Name', [validators.Length(min=10, max=250), pipeline_exists])

我得到的錯誤是:

TypeError: Unsupported type "<class 'wtforms.fields.core.UnboundField'>" for value "<UnboundField(StringField, (), {})>"

我找到了答案:我在鍵查找中缺少 .data 選項

def pipeline_exists(self, pipeline_name):
    ses = boto3.session.Session()
    try:
        dynamodb = ses.resource('dynamodb')
    except Exception as exception:
        message = 'Error on DynamoDB client'
        logging.error("Dynamodb client session failed: %d %s", exception)
        msg = " Dynamodb client session failed: " + str(exception)

        raise exception
    table_name = 'my_tbl'
    table = dynamodb.Table(table_name)

    response = table.query(
        KeyConditionExpression=Key('pipeline_name').eq(pipeline_name.data)
    )

    print(response)
    if response['count'] > 0:
        raise validators.ValidationError('Pipeline ' + pipeline_name.data + ' already exists')
    else:
        True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM