Schemas Nested fields
maus007 opened this issue · 1 comments
maus007 commented
Hello
I try make nested filed but got error
Traceback (most recent call last):
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/flask_restplus/api.py", line 319, in wrapper
resp = resource(*args, **kwargs)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/mishaserbenyuk/PycharmProjects/sshportal-api-1.0/flask_restplus_patched/namespace.py", line 58, in wrapper
return func_or_class(*args, **kwargs)
File "/Users/mishaserbenyuk/PycharmProjects/sshportal-api-1.0/flask_restplus_patched/namespace.py", line 155, in dump_wrapper
print model.dump(response).data
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/schema.py", line 530, in dump
**kwargs
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/marshalling.py", line 138, in serialize
index=(index if index_errors else None)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/marshalling.py", line 62, in call_and_store
value = getter_func(data)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/marshalling.py", line 132, in <lambda>
getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/fields.py", line 252, in serialize
return self._serialize(value, attr, obj)
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/fields.py", line 443, in _serialize
schema = self.schema
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/fields.py", line 427, in schema
dump_only=self._nested_normalized_option('dump_only'))
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/marshmallow/fields.py", line 438, in _nested_normalized_option
if field.startswith(nested_field)]
File "/Users/mishaserbenyuk/env/sshportal-api-python2.7/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 2950, in __bool__
raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined
My models looks like
class user_user_groups(db.Model):
__tablename__ = "user_user_groups"
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
user_group_id =db.Column(db.Integer, db.ForeignKey('user_groups.id'), primary_key=True)
user_group = db.relationship("user_groups",lazy="joined",backref=db.backref('user_groups'))
user = db.relationship("User",lazy="joined")
class User(db.Model):
""" User Model for storing user related details """
__tablename__ = "users"
id = db.Column(db.INTEGER, primary_key=True, autoincrement=True)
created_at = db.Column(db.TIMESTAMP, nullable=True)
updated_at = db.Column(db.TIMESTAMP, nullable=True)
deleted_at = db.Column(db.TIMESTAMP, nullable=True)
is_admin = db.Column(db.BOOLEAN(), nullable=True)
email = db.Column(db.String(255),nullable=True)
name = db.Column(db.String(255), unique=True)
comment = db.Column(db.String(255), nullable=True, default='')
invite_token = db.Column(db.String(255),nullable=True)
user_groups = db.relationship("user_user_groups",lazy="joined")
user_keys = db.relationship("user_keys",lazy="joined")
password = db.Column(db.String(255),nullable=False)
class user_groups(db.Model):
__tablename__ = "user_groups"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
created_at = db.Column(db.TIMESTAMP, nullable=True)
updated_at = db.Column(db.TIMESTAMP, nullable=True)
deleted_at = db.Column(db.TIMESTAMP, nullable=True)
name = db.Column(db.String(255), unique=True, nullable=True)
comment = db.Column(db.String(255),nullable=True)
crm_group_id = db.Column(db.Integer(),nullable=True)
users = db.relationship("user_user_groups",lazy="joined")
acls = db.relationship("user_group_acls",lazy="joined")
And I try in Schemas
class UserUserGroupSchema(ModelSchema):
class Meta:
model = user_user_groups
fields = (
user_user_groups.user_group_id.key,
user_user_groups.user_id.key,
)
dump_only = (
user_user_groups.user_id,
)
class BaseUserSchema(ModelSchema):
"""
Base user schema exposes only the most general fields.
"""
#user_groups = base_fields.Nested(UserUserGroupSchema)
class Meta:
# pylint: disable=missing-docstring
model = User
fields = (
User.id.key,
User.name.key,
User.email.key,
User.comment.key,
User.invite_token.key,
User.is_admin.key,
)
dump_only = (
User.id,
)
class DetailedUserSchema(BaseUserSchema):
"""
Detailed user schema exposes all useful fields.
"""
user_groups = base_fields.Nested("UserUserGroupSchema", many=True)
#print user_groups
class Meta(BaseUserSchema.Meta):
fields = BaseUserSchema.Meta.fields + (
'user_groups',
)
dump_only = (
User.id,
)
Can you help?
What am I doing wrong?
frol commented
It seems that the problem is that you pass the fields instead of field names into dump_only
. Change user_user_groups.user_id
to user_user_groups.user_id.key
and User.id
to User.id.key
in the dump_only
tuples.