Когда я пытаюсь сериализовать Python объекты в формате JSON, используя пакет marshmallow и marshmallow-enum AttributeError
запущен - 'str' object has no attribute 'name'
У меня есть следующие классы, связанные с внешними ключами:
class BusinessUnitsManagement(db.Model, DatabaseObject):
__tablename__ = 'bu_mngmt'
id = db.Column(db.Integer(), primary_key=True)
bu_id = db.Column(db.Integer(), db.ForeignKey('business_units.id'), nullable=False)
head = db.Column(db.Integer(), db.ForeignKey('user_properties.id'), nullable=True)
bu_properties = db.relationship('BusinessUnits', foreign_keys=[bu_id], backref='bu_mngmt')
head_properties = db.relationship('UserProperty', foreign_keys=[head])
class UserProperty(db.Model, DatabaseObject):
__tablename__ = 'user_properties'
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
seniority = db.Column(db.Enum(Seniority), nullable=True)
class BusinessUnits(db.Model):
__tablename__ = 'business_units'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(5), nullable=False)
description = db.Column(db.String(100), nullable=True)
А также я создаю классы схемы для этих классов:
from marshmallow import Schema, fields
from marshmallow_enum import EnumField
class BusinessUnitsManagementSchema(Schema):
id = fields.Int()
bu_properties = fields.Nested(BusinessUnitsSchema)
head_properties = fields.Nested(UserPropertySchema)
class UserPropertySchema(Schema):
id = fields.Int()
full_name = fields.Str()
seniority = EnumField(Seniority)
class BusinessUnitsSchema(Schema):
id = fields.Int()
name = fields.Str()
description = fields.Str()
Старшинство определяется как:
Seniority = Enum(
value='Seniority',
names=[
('Intern', 'Intern'),
('Junior_Engineer', 'Junior_Engineer'),
('Junior Engineer', 'Junior Engineer'),
('Design Engineer', 'Design Engineer'),
('Design_Engineer', 'Design_Engineer'),
('Senior Engineer', 'Senior Engineer'),
('Senior_Engineer', 'Senior_Engineer')
]
)
Когда я пытаюсь получить JSON формат Python объектов:
from marshmallow import Schema, fields
from marshmallow_enum import EnumField
objects = BusinessUnitsManagement.query.join(BusinessUnits).all()
result = []
for object in objects:
marshmallow_item = BusinessUnitsManagementSchema.dumps(object)
result.append(marshmallow_item.data)
return result
Я получаю эту ошибку в Ubuntu, но в Windows все в порядке:
marshmallow-enum version on both Win and Ubuntu --- 1.5.1
Python version on Win 10 ---------- 3.6.6
Python version on Ubuntu 16.04.3 -- 3.5.2
marshmallow_item = schema.dumps(item)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/schema.py", line 557, in dumps
deserialized, errors = self.dump(obj, many=many, update_fields=update_fields)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/schema.py", line 509, in dump
**kwargs
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/marshalling.py", line 138, in serialize
index=(index if index_errors else None)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/marshalling.py", line 62, in call_and_store
value = getter_func(data)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/marshalling.py", line 132, in <lambda>
getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/fields.py", line 252, in serialize
return self._serialize(value, attr, obj)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/fields.py", line 450, in _serialize
update_fields=not self.__updated_fields)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/schema.py", line 509, in dump
**kwargs
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/marshalling.py", line 138, in serialize
index=(index if index_errors else None)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/marshalling.py", line 62, in call_and_store
value = getter_func(data)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/marshalling.py", line 132, in <lambda>
getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow/fields.py", line 252, in serialize
return self._serialize(value, attr, obj)
File ".../flaskvenv/lib/python3.5/site-packages/marshmallow_enum/__init__.py", line 80, in _serialize
return value.name
AttributeError: 'str' object has no attribute 'name'