Skip to content
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

Add to_dict method to model classes #10

Closed
declaresub opened this issue Jan 3, 2022 · 3 comments
Closed

Add to_dict method to model classes #10

declaresub opened this issue Jan 3, 2022 · 3 comments

Comments

@declaresub
Copy link

I want to save heron transactions to a database in json format. Thus it would be convenient if the various model classes had a to_dict or similarly-named method that returns a dict representation of a model object. Preferably the dict returned could be passed to Model.init to reconstitute the object.

@cesarferradas
Copy link
Contributor

@declaresub thanks for the suggestion! You can always convert a python object into a dictionary by using vars(), so you could do that no problem. Because this is not always clear, I'm adding a to_dict() instance method for convenience in PR #11

@cesarferradas
Copy link
Contributor

This method is now available in version 0.1.3: https://pypi.org/project/heron-data/0.1.3/

@declaresub
Copy link
Author

Actually, you cannot always convert a python object into a dictionary using vars -- instances of a class created with a slots attribute, in particular. But the real problem with this implementation is that it doesn't fit the use case of serializing to JSON. A Transaction object has a merchant attribute of type Optional[Merchant]. This to_dict method will not convert the Merchant object to a dict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants