# How to make a copy of a connected class?

**URL:** <https://discuss.tryton.org/t/how-to-make-a-copy-of-a-connected-class/3117>\
**Category:** Developer\
**Created:** [August 13, 2020, 3:02am UTC](https://discuss.tryton.org/t/how-to-make-a-copy-of-a-connected-class/3117 "2020-08-13T03:02:57Z")\
**Posts on this page:** 4\
**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 13, 2020, 3:02am UTC](https://discuss.tryton.org/t/how-to-make-a-copy-of-a-connected-class/3117/1 "2020-08-13T03:02:57Z")

</div>

I connected Sale Orders (SO) to Purchase Orders (PO) by Many2Many. The reason for M2M — that is possible to collect many SO to one gigantic PO or vice versa :

```auto
purchases = fields.Many2Many('purchase.purchase-sale.sale', 'sale', 'purchase', 
        string = 'Related purchase orders', 
        help = 'Purchase orders as the result of this sale',
        )

```

 ![image](https://discuss-cdn.tryton.org/uploads/default/original/2X/5/5452ccb73824ef8eb78cb9d3948acb8f54f58198.png)

With that said, I want a user to be allowed to create a new PO based on the current SO, and then to auto-populate all fields of that ‘child’ PO with data from current SO.  
I don’t understand how to do that.  
Firstly, I tried to create a boolean checkmark (And I like this idea): when you check it, the on\_change method should create a new PO and then populate it.  
But there is the problem: I do not understand — how to create a new PO in the system?  
(Once created, I can add its ID to the set in the M2M field).

May you point me to the example of how to do that correctly? I realized that there are `cls. __setup__ ` and `cls. __register__ ` methods, but I don’t understand how to use them. If there is a good example of creating a new record in the DB (i.e. a new class instance), that will persist in the system?

Constantine.

---

<div class="post-metadata">

**Author:** ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)\
**Post date:** [August 13, 2020, 7:35am UTC](https://discuss.tryton.org/t/how-to-make-a-copy-of-a-connected-class/3117/2 "2020-08-13T07:35:06Z")

</div>

Hi,  
Here’s an example of a wizard to create a Purchase from a Purchase Request:  
[http://hg.tryton.org/modules/purchase\_request/file/tip/purchase\_request.py#l361](http://hg.tryton.org/modules/purchase_request/file/tip/purchase_request.py#l361)

---

<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:** [August 13, 2020, 8:15am UTC](https://discuss.tryton.org/t/how-to-make-a-copy-of-a-connected-class/3117/3 "2020-08-13T08:15:28Z")

</div>

> [@banknote](#):
>
> The reason for M2M — that is possible to collect many SO to one gigantic PO

I think you should use the `sale_supply` module and maybe customize the `purchase_request` module to group purchase based on your criteria.

---

<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 12, 2020, 8:15am UTC](https://discuss.tryton.org/t/how-to-make-a-copy-of-a-connected-class/3117/4 "2020-09-12T08:15:31Z")

</div>

Closed on September 12, 2020, 8:15am UTC

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