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

Pass context to other models when fetching more data #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

a-willem
Copy link

@a-willem a-willem commented Feb 7, 2022

When OdooRPC dynamically fetches more information (i.e. when accessing .partner_id.name), if the instance has been instantiated using with_context, this context is not passed along and can lead to unwanted results (i.e when using with_context(allowed_company_ids=[1,2,3]))

This fix allows to pass such context information.

@@ -316,6 +316,7 @@ def _create_model_class(self, model):
fields_get = self._odoo.execute(model, 'fields_get')
for field_name, field_data in fields_get.items():
if field_name not in FIELDS_RESERVED:
field_data['context'] = self._context # pass context to new fields ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for contributing.
Could you please share the use case that need to add context to field data

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @flotho,

In the following sample code (some bits are redundant, they're extracted from a longer chunk of code..), using a vanilla odoorpc==0.8.0, you will get term_id = False and fiscal_pos_id = False.

Using this proposed patch, you will get the id corresponding to the requested property, depending on the requested company_id.

    creds['companyid'] = <sample company_id>
    rp = odoo.env['res.partner']
    rp_list = rp.with_context(allowed_company_ids=[creds['companyid']])
    move_part = rp_list.browse([<sample partner_id>])
    term_id = move_part.with_context(company=creds['companyid']).property_payment_term_id.id
    fiscal_pos_id = move_part.with_context(company=creds['companyid']).property_account_position_id.id

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope my approach is the right one ! If I've missed something, don't hesitate to correct me.

Best regards,

Arnaud

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, more clear and very useful

@@ -316,6 +316,7 @@ def _create_model_class(self, model):
fields_get = self._odoo.execute(model, 'fields_get')
for field_name, field_data in fields_get.items():
if field_name not in FIELDS_RESERVED:
field_data['context'] = self._context # pass context to new fields ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, more clear and very useful

@marcwimmer
Copy link

Why is this not merged yet ?

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

Successfully merging this pull request may close these issues.

3 participants