2017-01-22 28 views
0

*this question is not a duplicate of another question and I have already tried the answer given in that question and it did not solve my problem. Thanks.Python SQLalchemy TypeError: 'list'オブジェクトは呼び出し可能ではありません

私はPythonの初心者で、SQLalchemyの使い方を学んでいます。

  1. 私は私のJSコードは、オブジェクトのリストを読んで、ビューに表示されます
  2. オブジェクトのリストとして結果を返す私のデータベーステーブル
  3. からすべての行を取得しようとしています

しかし、私はこのリストオブジェクトを取得している呼び出し可能なエラーではありません。誰かが私にこのガイドを与えることができますあなたに感謝してください。

@mod_core.route('/getDatatable', methods=['GET']) 
def datatable_get(): 
    # Query 'user' table. 
    _user = User.query.all() 
    _results = [] 
    for user in _user: 
     print user.get_all() 
     _results.append(user.get_all()) 
    return _results 

127.0.0.1 - - [22/Jan/2017 17:10:46] "GET /getDatatable HTTP/1.1" 500 - Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in call return self.wsgi_app(environ, start_response)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app response = self.make_response(self.handle_exception(e))

File "/usr/local/lib/python2.7/dist-packages/flask_restful/init.py", line 271, in error_router return original_handler(e)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app response = self.full_dispatch_request()

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request response = self.make_response(rv)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1746, in make_response rv = self.response_class.force_type(rv, request.environ)

File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 847, in force_type response = BaseResponse(*_run_wsgi_app(response, environ))

File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app return _run_wsgi_app(*args)

File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 871, in run_wsgi_app app_rv = app(environ, start_response)

TypeError: 'list' object is not callable

UPDATE 1:私も、返す前にJSONにユーザーを変更しようとしたし、まだ同じエラーを取得しています

@mod_core.route('/getDatatable', methods=['GET']) 
def datatable_get(): 
    # Query 'user' table. 
    _user = User.query.all() 
    results = [] 
    for user in _user: 
     results.append(user.to_json()) 
     print results 
    return results 

Userクラス

def to_json(self): 
     """ 
     Return a JSON response for RESTful API GET request. 
     """ 
     _user_in_josn = {'login_id': self.login_id, 
         'fullname': self.fullname, 
         'nickname': self.nickname, 
         'matric': self.matric_no, 
         'email': self.email, 
         'alt_email': self.alt_email 
      } 
     return _user_in_josn 

UPDATE 2:JSONにキャストしようとしました戻っ結果

User object at 0x7f88f80b14d0> is not JSON serializable

前は私を沿って案内してください。あなたの親切な注意と助けをありがとう。

+0

スタックトレースは、ハイテクお返事に感謝を – schematic

答えて

1

私はあなたの問題がから来ている場所を正確にわからないんだけど、私はあなたのuserオブジェクトでやっている紛らわしいものを参照しています。ループにuserを取得しましたが、依然としてuser.get_all()と呼び出しています。その時点で、userがただ1つのユーザーオブジェクトであるため、何が行われるべきかわかりません。 userのデータベースIDが必要ですか?ユーザー名のようなデータのいくつかの部分?あなたのリストにuserのIDを追加します

_results.append(user.id) 

はこのような何かにその行を変更してみてください。または、userオブジェクトに保存した他のデータを追加することもできます。あなたがreturnに、データベース内のすべてのユーザーの完全なリストが必要な場合は、次の操作を行います。私はまた、これらの変数_user_resultsの名前を変更することをお勧めかもしれません

return _user 

。先行するアンダースコアを使用するのは、通常は隠し変数のためだけであり、ここではその必要はありません。また、_user店の多くuserオブジェクト以来、usersような名前は、おそらく変数に何が含まれているかを示すために、より適切であろう。

+0

役立つだろう!データベース内のすべてのユーザーの完全なリストを返したいと思います。私はreturn _userを試しましたが、このエラーが出ます。TypeError: 'list'オブジェクトは呼び出し可能ではありません。 – Zainau

+0

あなたの関数が返すリストをどのように使用するかは、それが原因である可能性があります。コードのその部分を見て、返すデータの使い方を見てください。これは単なるリストなので、ループすることもできますし、 'users [0]'のような角括弧でインデックスすることもできますが、関数のように呼び出すことはできません( 'users()'のようになります) 。 – coralvanda

+0

あなたの返信ありがとう!私はangularJSを使ってフラスコにデータを求めました。 $ http.get( "/ getDatatable")。then(function(response){$ scope.tableresults = response.data} .... – Zainau

関連する問題