Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Adding calculated attribute to the scheme #290

Open
Bu3a3a opened this issue Feb 12, 2020 · 0 comments
Open

Adding calculated attribute to the scheme #290

Bu3a3a opened this issue Feb 12, 2020 · 0 comments

Comments

@Bu3a3a
Copy link

Bu3a3a commented Feb 12, 2020

Hi!
I'm trying to add a calculated field to the scheme using @post_dump:

from marshmallow_jsonapi import Schema as JSONApiSchema, fields


class MyScheme(JSONApiSchema):
    id = fields.Int(required=False)
    is_deleted = fields.Bool(required=True)
    is_enabled = fields.Bool(required=True)
    is_active = fields.Bool(required=False)

    class Meta:
        type_ = 'my_model'

    @post_dump
    def is_active(self, data):
        data['is_active'] = bool(not data['is_deleted'] and data['is_enabled'] ) 

I'm getting this error:

.../python3.7/site-packages/marshmallow_jsonapi/schema.py", line 355, in format_item
    attribute = attributes[field_name]
KeyError: 'is_active'

Is there a way to make it with marshmallow-jsonapi?

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