Is there a difference between a marshmallow.Schema (v3.0+) where a field with name foo is defined with attribute="bar", and another where a field with name bar is defined with data_key="foo"?
Both seem to serialize and deserialize dictionaries and other simple objects in the same way:
import marshmallow
class MySchema1(marshmallow.Schema):
foo = marshmallow.fields.String(attribute="bar")
class MySchema2(marshmallow.Schema):
bar = marshmallow.fields.String(data_key="foo")
schema1 = MySchema1()
schema2 = MySchema2()
dictionary = {"bar": "hello world"}
assert schema1.dump(dictionary) == schema2.dump(dictionary)
assert schema1.load(schema1.dump(dictionary)) == schema2.load(schema2.dump(dictionary))
class Thingy:
def __init__(self):
self.bar = "hello world"
instance = Thingy()
assert schema1.dump(instance) == schema2.dump(instance)
assert schema1.load(schema1.dump(instance)) == schema2.load(schema2.dump(instance))
The above passes. This isn't causing any errors in my project currently, but I am curious what the difference is! Thanks in advance.