sqlalchemy-mixins icon indicating copy to clipboard operation
sqlalchemy-mixins copied to clipboard

Relationship exclude to serializate

Open jeandelacruz opened this issue 2 years ago • 2 comments

I have run into the problem that, exclude to field relation, it does not do so.

First Model

class User(BaseModel):
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), index=True)
    shopping_carts = relationship(
        'ShoppingCart', uselist=True, back_populates='user'
    )

Second Model

class ShoppingCart(BaseModel):
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship(
        'User', uselist=False, back_populates='shopping_carts'
    )

Use smart query.

jeandelacruz avatar May 03 '22 22:05 jeandelacruz

@jeandelacruz Do you mind adding an example of the query you are running, what's being returned, and what's expected to be returned?

michaelbukachi avatar May 03 '22 22:05 michaelbukachi

@jeandelacruz Do you mind adding an example of the query you are running, what's being returned, and what's expected to be returned?

This code:

records = self.model.smart_query(
            filters={**filters},
            sort_attrs=['id'],
            schema={
                'role': JOINED,
                'shopping_carts': JOINED
            }
        ).paginate(
            per_page=query.per_page, page=query.page
        )

return jsonify(results=records.to_dict(nested=True, exclude=['shopping_carts']))

Results: download

Expected returned: download

jeandelacruz avatar May 03 '22 23:05 jeandelacruz