I have 2 tables with "one-to-one" relationship.
When I get data from one table I want to include one field from the related table but without "dot notation".
What works:
class UserEntitySchema(db_schema.Schema):
class Meta:
fields = ('id', 'username', 'email', 'confirmed', 'created', 'enabled', 'account.status')
I want "account.status" would come as just "status" but I can't figure out how to gain it.
I tried Pluck as @marke suggested but without any result. Anything is wrong here?
class AccountEntitySchema(db_schema.Schema):
current_status = fields.Str()
class Meta:
fields =('current_status',)
class UserEntitySchema(db_schema.Schema):
status = fields.Pluck(AccountSchema, 'current_status')
class Meta:
fields = ('id', 'username', 'email', 'status')
My updated, working solution (thanks @marke!):
class AccountEntitySchema(db_schema.Schema):
class Meta:
fields =('current_status',)
class UserEntitySchema(db_schema.Schema):
account = fields.Pluck(AccountSchema, 'current_status', data_key='status') #<==== data_key will replace the existing field
class Meta:
# Fields to expose
fields = ('id', 'username', 'email', 'account')
As mentioned in the comments, one can use pluck field: https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.fields.Pluck