python - Flask-Restless & Marshmallow: 'dict' object has no attribute '_sa_instance_state' -
i'm working on basic crud json rest api using flask-restless, flask-sqlalchemy , marshmallow.
i'm experiencing problem using combination of libraries, when enable deserialization using marshmallow.
i've extensively googled error, find similar issues when using database relations, i'm not.
i've been removing code possible application while still getting same error. following code blocks 1:1 what's running locally.
models.py:
from flask_sqlalchemy import sqlalchemy db = sqlalchemy() class case(db.model): __tablename__ = 'case' id = db.column(db.integer, primary_key=true) amount = db.column(db.integer, nullable=false)
serializers.py:
from marshmallow import schema class caseschema(schema): pass # simplicity - nothing works case_schema = caseschema() def case_serializer(instance): return case_schema.dump(instance).data def case_deserializer(data): return case_schema.load(data).data
main.py:
from flask import flask import flask_restless import models import serializers app = flask(__name__) app.config['debug'] = true models.db.init_app(app) def init_db(): app.app_context(): models.db.create_all() def init_api(): init_db() app.app_context(): api_manager = flask_restless.apimanager(app, flask_sqlalchemy_db=models.db) api_manager.create_api(models.case, methods=['get', 'post', 'put'], serializer=serializers.case_serializer, deserializer=serializers.case_deserializer) # <- stuff breaks when enabling line if __name__ == "__main__": init_db() init_api() app.run()
tests.py:
import json import os import myapp import unittest import tempfile myapp.app.app_context(): myapp.init_api() class myapptestcase(unittest.testcase): def setup(self): self.db_fd, myapp.app.config['database'] = tempfile.mkstemp() myapp.app.config['testing'] = true self.app = myapp.app.test_client() def teardown(self): os.close(self.db_fd) os.unlink(myapp.app.config['database']) def test_create_case(self): msg = { "amount": 1038, } post_response = self.app.post("/api/case", data=json.dumps(msg), content_type="application/json") assert post_response.status_code == 201 if __name__ == '__main__': unittest.main()
versions in use:
flask==0.11 flask-sqlalchemy==2.1 flask-restless==0.17.0 mysqlclient==1.3.7 sqlalchemy-utils==0.32.8 marshmallow==2.9.0
finally, full stacktrace when running tests:
====================================================================== error: test_create_case (__main__.myapptestcase) ---------------------------------------------------------------------- traceback (most recent call last): file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 1584, in add state = attributes.instance_state(instance) attributeerror: 'dict' object has no attribute '_sa_instance_state' during handling of above exception, exception occurred: traceback (most recent call last): file "myapp_tests.py", line 37, in test_create_case content_type="application/json") file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/werkzeug/test.py", line 788, in post return self.open(*args, **kw) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/testing.py", line 113, in open follow_redirects=follow_redirects) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/werkzeug/test.py", line 751, in open response = self.run_wsgi_app(environ, buffered=buffered) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/werkzeug/test.py", line 668, in run_wsgi_app rv = run_wsgi_app(self.application, environ, buffered=buffered) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/werkzeug/test.py", line 871, in run_wsgi_app app_rv = app(environ, start_response) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__ return self.wsgi_app(environ, start_response) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app response = self.make_response(self.handle_exception(e)) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception reraise(exc_type, exc_value, tb) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise raise value file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app response = self.full_dispatch_request() file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request rv = self.handle_user_exception(e) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise raise value file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request rv = self.dispatch_request() file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask_restless/views.py", line 157, in decorator return func(*args, **kw) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/mimerender.py", line 244, in wrapper result = target(*args, **kwargs) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/views.py", line 84, in view return self.dispatch_request(*args, **kwargs) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask/views.py", line 149, in dispatch_request return meth(*args, **kwargs) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask_restless/views.py", line 189, in wrapped return func(*args, **kw) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/flask_restless/views.py", line 1449, in post self.session.add(instance) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/sqlalchemy/orm/scoping.py", line 157, in return getattr(self.registry(), name)(*args, **kwargs) file "/users/myuser/.virtualenvs/myenv/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 1586, in add raise exc.unmappedinstanceerror(instance) sqlalchemy.orm.exc.unmappedinstanceerror: class 'builtins.dict' not mapped
turns out had add __tablename__
model definition , extend schema:
class caseschema(schema): [...] @post_load def make_case(self, data): return case(**data)
Comments
Post a Comment