Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

ordered = True from marshmallow overriden by EnumField() in Schema #42

Open
Carton32 opened this issue Sep 2, 2020 · 0 comments
Open

Comments

@Carton32
Copy link

Carton32 commented Sep 2, 2020

Hi,

I'm using marshmallow_enum to handle the serialization of my Enum.

I have the following code :

class EntitySchema(ma.SQLAlchemyAutoSchema):

    class Meta:

        ordered = True

    def on_bind_field(self, field_name, field_obj):

        field_obj.data_key = camelcase(field_obj.data_key or field_name)

class Meter(Entity):

    model = db.Column(db.Enum(MeterModel), nullable=False)
    description = db.Column(db.String(100))
    location = db.Column(db.String(100))
    bus_protocol = db.Column(db.Enum(BusProtocol), nullable=False)
    bus_serial_address = db.Column(db.String(20), nullable=False)
    primary_address = db.Column(db.Integer, nullable=False)
    secondary_address = db.Column(db.Integer)
    baudrate = db.Column(db.Integer, nullable=False)
    bytesize = db.Column(db.Integer, default=8, nullable=False)
    parity = db.Column(db.String(1), nullable=False)
    stopbits = db.Column(db.Integer, nullable=False)
    password = db.Column(db.Integer)

class MeterSchema(EntitySchema, ma.SQLAlchemyAutoSchema):

    model = EnumField(MeterModel)
    bus_protocol = EnumField(BusProtocol)

    class Meta:
        
        #fields = ("id", "remote_id", "created_at", "updated_at", "model", "description", "location", "bus_protocol", "bus_serial_address", "primary_address", "secondary_address", "baudrate", "bytesize", "partiy", "stopbits", "password")
        model = Meter

When I retrieve a meter from my SQLite3 DB, I get the following JSON :

{
    "model": "SDM630V2CT",
    "busProtocol": "MODBUS",
    "id": 1,
    "remoteId": null,
    "createdAt": "2020-09-02T08:00:30",
    "updatedAt": "2020-09-02T08:00:30",
    "description": "Test meter",
    "location": "Office",
    "busSerialAddress": "/dev/ttyUSB0",
    "primaryAddress": 1,
    "secondaryAddress": null,
    "baudrate": 38400,
    "bytesize": 8,
    "parity": "N",
    "stopbits": 1,
    "password": null
}

As we can see, with "fields" commented out, "model" and "busProtocol" get on top of the JSON and don't respect anymore ordered = True inherited from EntitySchema. I would love to have all my JSON completely ordered and if I don't have to use fields = (...), it would be awesome because I don't want to have to change at multiple points the name of my DB fields.

Maybe it's an issue or maybe something exists to avoid this but I didn't find anything.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant