首页 > 请问用sqlalchemy如何根据中文字段名做查询?

请问用sqlalchemy如何根据中文字段名做查询?

数据库有个中文字段名叫'名称',如何根据这个中文字段做搜索 。

result=Minicomputer.query.filter_by(u'名称'='CC670a').first()

报错如下

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    app = create_app(os.getenv('FLASK_CONFIG') or 'default')
  File "/mnt/hgfs/python/flask/Project/__init__.py", line 34, in create_app
    from .main import main as main_blueprint
  File "/mnt/hgfs/python/flask/Project/main/__init__.py", line 3, in <module>
    from . import views, errors
  File "/mnt/hgfs/python/flask/Project/main/views.py", line 95
    result=Minicomputer.query.filter_by(u'名称'='CC670a').first()
SyntaxError: keyword can't be an expression

不应该是result=Minicomputer.query.filter_by(名称='CC670a').first()这样的吗?


函数参数形如key=value的key必须是标识符,而不能是表达式


做一个表名的映射即可

class Minicomputer(Base):
    __table__ = Base.metadata.tables['minicomputer']
    name = __table__.c[u'名称']


    def __repr__(self):

        return '<Minicomputer %r>' % self.name
【热门文章】
【热门文章】