# How to manually populate Many2Many?

**URL:** <https://discuss.tryton.org/t/how-to-manually-populate-many2many/3099>\
**Category:** Developer\
**Created:** [August 5, 2020, 6:17pm UTC](https://discuss.tryton.org/t/how-to-manually-populate-many2many/3099 "2020-08-05T18:17:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)\
**Post date:** [August 5, 2020, 6:17pm UTC](https://discuss.tryton.org/t/how-to-manually-populate-many2many/3099/1 "2020-08-05T18:17:50Z")

</div>

I have the following code (overriding a purchase module):

```auto
class Purchase(metaclass=PoolMeta):
    'Purchase'
    __name__ = 'purchase.purchase'

    items = fields.Many2Many('purchase.purchase-my_module.item', 'purchase', 'item', 
        string = 'Related items', 
        help = 'Items related to this Purchase',
        )

    sales = fields.Many2Many('purchase.purchase-sale.sale', 'purchase', 'sale', 
        string = 'Related sale orders', 
        help = 'Sale orders sources',
        )

```

I want to populate `items` field with items that I can get from the `sale.sale.items` (I also added `items=M2M()` in `sale.sale` class).  
The general idea is to connect `items` from `sales` to corresponded `purchases`.  
These items are not saleable/purchaseble items, but just some items that are owned by clients and are the copies of my own class in `my_module`.

Constantine.

---

<div class="post-metadata">

**Author:** ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)\
**Post date:** [August 5, 2020, 11:45pm UTC](https://discuss.tryton.org/t/how-to-manually-populate-many2many/3099/2 "2020-08-05T23:45:10Z")

</div>

So, I found the way how to do it. Hope the following short example would help others who will search a solution for the same task:

```auto
class Purchase(metaclass=PoolMeta):
    'Purchase'
    __name__ = 'purchase.purchase'

    items = fields.Many2Many('purchase.purchase-my_module.item',...)
    sales = fields.Many2Many('purchase.purchase-sale.sale',...)

    # This method is executed when user add/remove from the list of Sale orders in Purchases module 
    @fields.depends('sales') # Many2Many() | the list of sale orders that are origins for the purchase Many2Many
    @fields.depends('items') # Many2Many() | this the list of items I need to fill from sale orders 
    def on_change_sales(self): 
        items_set = {} # create an empty set, I dont need to keep history 
        for sale in self.sales: # in sale orders 'items' is also Many2Many() field
            for item in sale.items: # in each sale order I collect all items
                items_set[item.id] = item; # add each to the set
        self.items = items_set # Eventually I return the set to the purchase.items field. self.items = items_set # Eventually I return the set to the purchase.items field.
        return

```

Hope that helps to others.

Constantine.

---

<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:** [September 4, 2020, 11:45pm UTC](https://discuss.tryton.org/t/how-to-manually-populate-many2many/3099/3 "2020-09-04T23:45:15Z")

</div>

Closed on September 4, 2020, 11:45pm UTC

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