1

我创建了一个带有 SQLAlchemy 后端的 Flask 应用程序,它非常简单。但是,我在使用 Flask Admin 扩展时遇到了问题。虽然它适用于大多数模型,但其中一些给我以下错误:

TypeError: 'unicode' object is not callable

以下是其中一个问题模型的示例:

class Trade(db.Model):

    __tablename__ = 'trade'

    id = db.Column(db.Integer, primary_key=True)

    issuer_id = db.Column(db.Integer,
                          db.ForeignKey('issuer.id'), primary_key=True)

    obligation_id = db.Column(db.Integer,
                              db.ForeignKey('obligation.id'))

    obligation = db.relationship('Obligation', backref='trades')

    type = db.Column(db.String(50))
    purpose = db.Column(db.String(50))

    date = db.Column(db.Date)
    currency = db.Column(db.Text)
    amount = db.Column(db.Float)

    __mapper_args__ = {
        'polymorphic_on': purpose,
        'polymorphic_identity': 'trade',
        'with_polymorphic': '*'
    }

    def __str__(self):
        return self.obligation + ' ' + self.date.strftime('%y-%m-%d')

为了提供一些关于扩展如何调用它的上下文,我有一些代码可以为我的所有 sqlalchemy 模型创建管理页面。

from test_app import app
from flask.ext.admin import Admin
from test_app.db import db, get_model, models_list

from flask.ext.admin.contrib import sqla

admin = Admin(app, name='Test App')

for model in sorted(models_list()):

    admin_cls = type(model, (sqla.ModelView,), {'column_display_pk': False})
    admin_view = admin_cls(get_model(model), db.session)
    admin.add_view(admin_view)

最终结果是,当我导航到该模型的管理页面时,我收到以下错误,这并没有提供太多关于哪一行代码导致问题的上下文。

File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/user/app/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/base.py", line 60, in inner
return f(self, *args, **kwargs)
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/base.py", line 60, in inner
return f(self, *args, **kwargs)
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/base.py", line 60, in inner
return f(self, *args, **kwargs)
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/model/base.py", line 1182, in index_view
actions_confirmation=actions_confirmation)
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/base.py", line 254, in render
return render_template(template, **kwargs)
File "/home/user/app/venv/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/home/user/app/venv/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/home/user/app/venv/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/home/user/app/venv/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/model/list.html", line 4, in top-level template code
{% import 'admin/actions.html' as actionlib with context %}
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/master.html", line 1, in top-level template code
{% extends admin_base_template %}
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/base.html", line 22, in top-level template code
{% block page_body %}
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/base.html", line 50, in block "page_body"
{% block body %}{% endblock %}
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/model/list.html", line 49, in block "body"
{% block model_list_table %}
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/model/list.html", line 95, in block "model_list_table"
{% block list_row scoped %}
File "/home/user/app/venv/lib/python2.7/site-packages/flask_admin/templates/admin/model/list.html", line 118, in block "list_row"
<td>{{ get_value(row, c) }}</td>
TypeError: 'unicode' object is not callable

任何有关如何调试的建议将不胜感激。

非常感谢。

更新:

我发现了这个错误。它与@property 装饰器有关。在模型定义中,我有两个属性。

@property
def issue(self):

    return str(self.description)

@property
def __str__(self):

    return self.issue

如果您删除str上方的 @property,那么该错误就会消失。

https://gist.github.com/bfcondon/8760010

4

1 回答 1

1

这是因为 Flask-Admin 想要__unicode__来自 Python 2 模型的字符串。

尝试在您的模型中更改您的__str__to __unicode__,应用程序将起作用。同时,我将更新示例以更兼容 Python 2。

在 python 3 上,__str__不应该有@property装饰器。

于 2014-02-02T08:53:59.423 回答