How would this work if I want to fetch data from multiple models and send in a single response object. Kind of like having a data formed by SQL join statements, which joins multiple tables and creates a response data, how would such a data by serialized.
Hi there very good question and very common use case. In marshmallow you can have nested schemas within schemas like you have fields for example if you have a user that has many addresses as well as a single profile you would do the following: from marshmallow import Schema, fields class AddressSchema(Schema): city = fields.String() street = fields.String() postcode = fields.String() class ProfileSchema(Schema): dob = fields.DateTime() language = fields.String() class UserSchema(Schema): name = fields.String() email = fields.Email() created_at = fields.DateTime() addresses = fields.Nested(AddressSchema, many=True) profile = fields.Nested(ProfileSchema) Make sure to eager load the related models on the user in the view to not get n+1 queries! Hope this helps, if you have any questions let me know!
This nested things work like sort of inheritence right? from extensions import ma from models.users import User class UserSchema(ma.SQLAlchemyAutoSchema): class Meta: model = User exclude = ["id", "is_deleted", "is_active", "created_at", "modify_at"] class UserInfo(UserSchema): class Meta: exclude = ["hashed_password"] so when i get info of user i don't leak password. UserSchema will be use for creating user and Userinfo to get limited info of users