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

Convention converting a pydantic model to odoo vals #1427

Open
sebastienbeau opened this issue Oct 15, 2023 · 3 comments
Open

Convention converting a pydantic model to odoo vals #1427

sebastienbeau opened this issue Oct 15, 2023 · 3 comments

Comments

@sebastienbeau
Copy link
Contributor

Hi @shopinvader/shopinvader-maintainers

I would like you thoughts, regarding the best and cleanest way to convert a pydantic model into a dict of vals for odoo.

I see that two way have been used.

Converting the pydantic object into a vals into the pydantic class it's self

See here :

def to_res_partner_vals(self) -> dict:
vals = {
"name": self.name,
"street": self.street,
"street2": self.street2,
"zip": self.zip,
"city": self.city,
"phone": self.phone,
"email": self.email,
"state_id": self.state_id,
"country_id": self.country_id,
}

Or in the "service"

See here (complex conversion) :

self._apply_transactions(cart, transactions)

See here : https://github.com/shopinvader/odoo-shopinvader/pull/1421/files#diff-2bc91465c2d564fb6b38ffc94e181249d747a657a72b732b7a535902dab66e5aR240

@lmignon what do you think ?

@lmignon
Copy link
Collaborator

lmignon commented Oct 16, 2023

@sebastienbeau It depends.

When a Pydantic model is 'linked' to an odoo model, A method to_odoo_model into the pydantic model makes sense.
In the case of the Transaction model, this model is not linked to an odoo model. It's a set of information required by an route endpoint to do some logic. In such a case, it makes sens that the conversion occurs into the service layer. (Pydantic model instances should not be propagated outside the service layer)

Regarding, your complex version, IMO it's too complex. You could have a 'convert_to_my_model_write' method defined on your pydantic model. At least, don't use the result of a call to model_dump(exclude_unset=True) as base to build the dictionary you'll use to call the method write on your odoo model. This approach introduce a deep coupling between your pydantic model and your odoo model. If your model is extended to add some specific fields that are not expected by odoo, your code will be broken.

@sebastienbeau
Copy link
Contributor Author

Thanks for your feedback.
I am going to adapt the PR with your recommendation.
Thanks

@sebastienbeau
Copy link
Contributor Author

I have done some change in my PR.

Here is how to convert "input params" into domain :
169b339#diff-c16e014ac95e1ac4ac046d51df771bcd5b6d4bb2a989646b11ad1a123591bd32R12

Here is how I have converted "input params" to vals for the write
169b339#diff-58ebe71b902dc742a85164f8e89fb500c247c88eb0aca39d334e565d08923377R33

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

No branches or pull requests

2 participants