# AttributeError upon Access to Present Attribute

**URL:** <https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297>\
**Category:** Developer\
**Created:** [June 26, 2023, 11:02am UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297 "2023-06-26T11:02:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGM](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@AGM](https://discuss.tryton.org/u/AGM)\
**Post date:** [June 26, 2023, 11:02am UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/1 "2023-06-26T11:02:46Z")

</div>

Hello,

using trytond V 6.8.1 I tried to add a Function field to access the supplier’s product code.

I cannot make the following method working as it raises an:

`AttributeError: 'purchase.product_supplier' Model has no attribute 'code': <trytond.model.model.purchase.product_supplier._record object at ...>`

The access to the purchase\_product\_supplier.code attribute is validated by the preceding if statement. Hence I would not expect an error.

Here is the code:

```
purchase_product_supplier_code = fields.Function(
    fields.Char('Supplier_Code'),
    'get_purchase_product_supplier_code')

# lines omitted (....)

def get_purchase_product_supplier_code(self, name):
    """Return the supplier code of the product for the supplier of the
    quotation."""

    pool = Pool()
    ProductSupplier = pool.get('purchase.product_supplier')
    # get code from purchase.product.supplier where party = quotation.supplier and product = self.product
    if self.quotation and self.quotation.supplier and self.product:
        purchase_product_supplier = ProductSupplier(
                party=self.quotation.supplier.id,
                product=self.product.id,
                company=self.quotation.company)
        if purchase_product_supplier and 'code' in dir(purchase_product_supplier):
            return purchase_product_supplier.code

```

---

<div class="post-metadata">

**Author:** ![rvong](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@rvong](https://discuss.tryton.org/u/rvong)\
**Post date:** [June 26, 2023, 1:31pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/2 "2023-06-26T13:31:29Z")

</div>

I believe in your code:

```auto
    # get code from purchase.product.supplier where party = quotation.supplier and product = self.product
    if self.quotation and self.quotation.supplier and self.product:
        purchase_product_supplier = ProductSupplier(
                party=self.quotation.supplier.id,
                product=self.product.id,
                company=self.quotation.company)

```

You are not loading the specific purchase product supplier, but instead you are creating a new instance, hence member attribute **code** has not been initialized with any value and therefore the error.  
If you want to load the purchase product supplier from the DB with the specific supplier id, product id and company, you should use the search function. I.e.:

```auto
# get code from purchase.product.supplier where party = quotation.supplier and product = self.product
    if self.quotation and self.quotation.supplier and self.product:
      purchase_product_suppliers = ProductSupplier.search([
        ('party', '=', self.quotation.supplier.id),
        ('product', '=', self.product.id),
        ('company', '=', self.quotation.company)
      ], limit = 1)
      if purchase_product_suppliers:
        purchase_product_supplier = purchase_product_suppliers[0] 
     ....

```

I hope this helps.

---

<div class="post-metadata">

**Author:** ![AGM](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@AGM](https://discuss.tryton.org/u/AGM)\
**Post date:** [June 26, 2023, 1:44pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/3 "2023-06-26T13:44:24Z")

</div>

It helped! Thank you very much.

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [June 26, 2023, 2:16pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/4 "2023-06-26T14:16:24Z")

</div>

The proper way to retrieve product suppliers from a product is by calling `product.product_suppliers_used()` with the proper pattern.

---

<div class="post-metadata">

**Author:** ![AGM](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@AGM](https://discuss.tryton.org/u/AGM)\
**Post date:** [June 26, 2023, 2:58pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/5 "2023-06-26T14:58:00Z")

</div>

> [@ced](#):
>
> product\_suppliers\_used

If the `ProductSupplier.search(...)` method call is replaced by:

`purchase_product_suppliers = self.product.product_suppliers_used({'party': self.quotation.supplier.id})`

I get:

```auto
    purchase_product_suppliers = self.product.product_suppliers_used(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Product.product_suppliers_used() takes 1 positional argument but 2 were given

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [June 26, 2023, 3:02pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/6 "2023-06-26T15:02:01Z")

</div>

It must be:

```auto
self.product.product_suppliers_used(party=self.quotation.supplier.id, company=self.quotation.company.id)

```

---

<div class="post-metadata">

**Author:** ![AGM](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@AGM](https://discuss.tryton.org/u/AGM)\
**Post date:** [June 26, 2023, 3:26pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/7 "2023-06-26T15:26:06Z")

</div>

> [@ced](#):
>
> `self.product.product_suppliers_used(party=self.quotation.supplier.id, company=self.quotation.company.id)`

Thank you, I switched over to your suggestion.

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [July 26, 2023, 3:26pm UTC](https://discuss.tryton.org/t/attributeerror-upon-access-to-present-attribute/6297/8 "2023-07-26T15:26:18Z")

</div>

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