jaimegildesagredo / booby

Data modeling and validation Python library
https://booby.readthedocs.org
Other
177 stars 18 forks source link

Creating a decoder mixin #27

Closed lorddaren closed 9 years ago

lorddaren commented 9 years ago

What would be the best way to create my own decoder mixin? I would like to change the name of the field during decoding to match an API's custom field value instead of the real name.

Is this possible?

jaimegildesagredo commented 9 years ago

Hi @dlord781 and sorry for the delay.

To change the field name in encoding/decoding you could use the name argument in fields [0].

from booby import Model, fields

class Foo(Model):
    some_name = fields.Field(name='somName')

Foo(some_name='bar).encode()   # {'someName': 'bar'}

If you need a custom encoder/decoder for a custom behavior you can create a base model, or some mixing classes with endcode and decode methods [1]. Also you can pass encoders to a field [2].