Flask Marshmallow - 模式返回属性错误?

Flask Marshmallow - schema returning an attribute error?

我正在尝试 return 数据库中所有关键字的列表,看起来像

  [
    'foo',
    'bar',
    'baz',
  ]

型号

class Keyword(db.Model, SurrogatePK):
    __tablename__ = 'keywords'
    keyword = Column(db.Text, nullable=False, unique=True)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<Keyword({keyword})>'.format(keyword=self.keyword)

架构

class KeywordSchema(ma.ModelSchema):
    class Meta:
        model = Keyword

那么,

keywords_schema = KeywordSchema(many=True, only='keyword')

路线

class Keywords(Resource):
    def get(self):
        '''Head to http://localhost:5000/api/v1/keywords

        Returns a list of all keywords.

        '''
        keywords = Keyword.query.all()
        result = keywords_schema.dump(keywords)
        return jsonify({'keywords': result})

这是抛出一个 `AttributeError'

部分追踪

File "C:\Users\ddonovan\DHG\web\flask-research\Query-Metadata\metadata\views.py", line 33, in get
    result = keywords_schema.dump(keywords)
  File "c:\users\ddonovan\.edm\envs\flask\lib\site-packages\marshmallow\schema.py", line 491, in dump
    self._update_fields(processed_obj, many=many)
  File "c:\users\ddonovan\.edm\envs\flask\lib\site-packages\marshmallow\schema.py", line 760, in _update_fields
    ret = self.__filter_fields(field_names, obj, many=many)
  File "c:\users\ddonovan\.edm\envs\flask\lib\site-packages\marshmallow\schema.py", line 834, in __filter_fields
    '"{0}" is not a valid field for {1}.'.format(key, obj))
AttributeError: "w" is not a valid field for <Keyword(numpy)>

我的 KeywordSchema 实例需要 keywords_schema = KeywordSchema(many=True, only=['keyword'])

然后在我的端点

class Keywords(Resource):
    def get(self):
        '''Head to http://localhost:5000/api/v1/keywords

        Returns a list of all keywords.

        Example Output:

        [
            "foo",
            "bar",
            "baz"
        ]

        '''
        keywords = Keyword.query.all()
        result = keywords_schema.dump(keywords)
        return jsonify([d['keyword'] for d in result.data])