Building a ProductCategory

Hi,

If I use a function to create a ProductCategory:

def _create_account_category(accounts, tax):
    ProductCategory = Model.get('product.category')

    account_category = ProductCategory(name="Account Category")
    account_category.accounting = True
    expense = accounts['expense']
    revenue = accounts['revenue']
    account_category.account_expense = expense
    account_category.account_revenue = revenue
    account_category.customer_taxes.append(tax)
    return account_category

It works like a charm, however, If I try to create the same ProductCategory (with identical input params) through a builder:

class ProductCategoryBuilder:

    def __init__(self, **kwargs):
        self._result = Model.get('product.category')(**kwargs)

    def append_customer_tax(self, tax):
        self._result.customer_taxes.append(tax)
        return self

    def build(self):
        return self._result

I get an error like this:

assert record._group is None

when code try to execute that method from buider:

def append_customer_tax(self, tax):

Thoughts?

You have to reload the tax record before append it to another customer_taxes category. It happens in proteus with any record added to a m2m relation.

tax = Tax(tax.id)

Thank you very much.

Only there? Can you link me to any documentation resource where I can study about that kind of “tricks”?

proteus mimic the behavior of the client. So you can only do what is possible to do in the client.
So in the client you can not put the same record in different fields, you always go through a search (which returns new instances).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.