-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load Models from JOIN as Dictionary #724
Comments
How about just If you really want to leverage the loader system to serialize the rows directly, you may want to take a look at the CallableLoader and write your own serializer: subloader = orm.Product.load(brand=orm.Brand)
def serialize(row, context):
product = subloader.do_load(row, context)
return dict(product.to_dict(), brand=product.brand.to_dict())
serialized_products = await query.gino.load(serialize).all() I didn't test this, but I think it explains the idea. |
Thanks for the answer. In my case, I'm using gino with fastapi, so I'm returning the result as a JSON. With the first method, simply taking the result and converting it does not work
Gives me an error because I was aware of the loaders, but didn't think they fit my purpose. Will try and let you know. Thanks |
Description
The case is simple: a table represents
product
(each with one brand attribute), pointing at abrand
table.When joining the two tables, I would like to get the
brand
information in the product, as a dictionary (or JSON, doesn't matter since I'm developing a rest api).The same case could be applied to
product
andcategory
, where in this case a many-to-one relationship is used. Instead of having a dictionary representing the category, an array of dictionaries (representing the categories) could be used.What I Did
In this case an error is triggered on
to_dict()
method since it requires the positional argument 'self'Just returning an attribute is possible as of now (just change
to_dict()
into an attribute name), but I need the complete information to be JSON serializable (via a dictionary or array of dictionaries) in order to be consumed by a client.Can this be achieved, in a simple manner, with GINO?
Thanks
The text was updated successfully, but these errors were encountered: