You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The case is simple: a table represents product (each with one brand attribute), pointing at a brand 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 and category, 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.
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:
How about just orm.Product.load(brand=orm.Brand)? It'll set orm.Brand instances on Product.brand for you.
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:
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
res = [x.to_dict() for x in products]
Gives me an error because orm.Brand is not serializable. to_dict() does not perform a deep conversion to dictionary so that it can be transformed into a serializable object.
I was aware of the loaders, but didn't think they fit my purpose. Will try and let you know.
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: