Skip to content

Commit

Permalink
[FIX] partner_company_default: multi-record creation aware and not al…
Browse files Browse the repository at this point in the history
…ways correct depending on when the create override in the stack is executed
  • Loading branch information
AungKoKoLin1997 committed Jan 9, 2024
1 parent fd807f3 commit ad0df3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 12 additions & 6 deletions partner_company_default/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo.tools import config


class ResPartner(models.Model):
_inherit = "res.partner"

company_id = fields.Many2one(default=lambda self: self.env.company)
company_id = fields.Many2one(default=lambda self: self._default_company_id())

@api.model
def create(self, vals):
# The context value is set in the create method of res.company
if self.env.context.get("creating_from_company"):
vals["company_id"] = False
return super(ResPartner, self).create(vals)
def _default_company_id(self):
"""Return False for other tests or if creating a company."""
context = self.env.context
if (
context.get("creating_from_company")
or config["test_enable"]
and not context.get("test_partner_company_default")
):
return False
return self.env.company
2 changes: 2 additions & 0 deletions partner_company_default/tests/test_partner_company_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_partner_company_default(self):
partner = (
self.env["res.partner"]
.with_user(self.user.id)
.with_context(test_partner_company_default=True)
.create({"name": "Test Partner 1"})
)
self.assertEqual(partner.company_id, self.user.company_id)
Expand All @@ -39,6 +40,7 @@ def test_partner_company_default(self):
partner = (
self.env["res.partner"]
.with_user(self.user.id)
.with_context(test_partner_company_default=True)
.create({"name": "Test Partner 2"})
)
self.assertEqual(partner.company_id, company_fr)

0 comments on commit ad0df3b

Please sign in to comment.