# Create new phones over existing ones for each party contact\_mechanism ( migration )

**URL:** <https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273>\
**Category:** Developer\
**Tags:** party\
**Created:** [September 24, 2020, 4:21pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273 "2020-09-24T16:21:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel\_dev](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/daniel_dev/32/975_2.png) [@daniel\_dev](https://discuss.tryton.org/u/daniel_dev)\
**Post date:** [September 24, 2020, 4:21pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/1 "2020-09-24T16:21:15Z")

</div>

Hi ! I am tryng to update existing phones list and add some new ones over a script and what i can do is to create new parties with all the data, but what i can’t do is to update for each partie the extra phone number.

What i succeded is to migrate multiple addreses for each partie.  
What i can’t understand, is how should i do to add each phone for each “original” partie.

I am sure that some of you allready did this before 🙂 please help ! any piece of code to see how is done? or explanation ? anything that could help ! thanks in advance !

---

<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:** [September 24, 2020, 4:25pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/2 "2020-09-24T16:25:18Z")

</div>

Contact mechanism are linked to party through a `One2Many` `contact_mechanisms`. So you just have to fill the field with as many instances as needed.

---

<div class="post-metadata">

**Author:** ![daniel\_dev](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/daniel_dev/32/975_2.png) [@daniel\_dev](https://discuss.tryton.org/u/daniel_dev)\
**Post date:** [September 24, 2020, 4:58pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/3 "2020-09-24T16:58:49Z")

</div>

Thanks for answering so fast Cédric !  
That, understanding that i have to do for example:

```auto
p = Party()
mechanism = p.contact_mechanism.new()
mechanism.save() # but this didn't work well.. 

```

tried also:

```auto
mechanism = ContactMechanism.find([])

if Party.find(['id', '=', ids_from_xslx_file[i]], limit=1)
for cm in mechanism: 
     try: 
          if phone_number.startswith(..):
                 cm.value = phone_number
                 cm.type = 'phone'  
                 cm.save()

```

But this one overwrites the existing phones.. not so good. Maybe i didn’t understood exactly how it works internally. Also i am kind of noob with Python in general but I’m willing to learn more 🙂

---

<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:** [September 24, 2020, 5:10pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/4 "2020-09-24T17:10:21Z")

</div>

> [@daniel\_dev](#):
>
> `mechanism.save() # but this didn't work well.. `

You have to save the party, you just created.

> [@daniel\_dev](#):
>
> But this one overwrites the existing phones…

Of course as you update existing mechanism.  
If you want new mechanism, you must instantiate new record.

---

<div class="post-metadata">

**Author:** ![daniel\_dev](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/daniel_dev/32/975_2.png) [@daniel\_dev](https://discuss.tryton.org/u/daniel_dev)\
**Post date:** [September 24, 2020, 8:03pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/5 "2020-09-24T20:03:11Z")

</div>

```auto
       
        for i in range(len(df)):
            if Party.find(['code', '=', "C" + sanitize(df['ID_Tryton'][i])], limit=1):
                p = Party()   
                cm = p.contact_mechanisms.new()
                try:
                    if sanitize_phone(df['TELF1'][i]).startswith('9'):
                        cm.value = sanitize_phone(df['TELF1'][i])
                        cm.type = 'phone'
                    elif sanitize_phone(df['TELF1'][i]).startswith('6'):
                        cm.value = sanitize_phone(df['TELF1'][i])
                        cm.type = 'mobile'
                except Exception as e:
                    print(e)
                    pass
                to_save.append(p)
        Party.save(to_save)`

```

I think I’m getting close to solve it 😃  
Parties are created, but as new ones, not by adding each phone number for an existing party wich i try to find it by internal code as i have stored clients as C+ inicial\_id from the migration file.xlsx.

```
[proteus.Model.get('party.party')(2454),
 proteus.Model.get('party.party')(2455),
 proteus.Model.get('party.party')(2456),
 proteus.Model.get('party.party')(2457),
 proteus.Model.get('party.party')(2458),
 proteus.Model.get('party.party')(2459),
 proteus.Model.get('party.party')(2460),
 proteus.Model.get('party.party')(2461),

```

…

---

<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:** [September 24, 2020, 8:16pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/6 "2020-09-24T20:16:22Z")

</div>

Of course that you create new party because:

> [@daniel\_dev](#):
>
> ` p = Party()`

If you want to add to the searched parties, you must use the result of you `Party.find` call.

---

<div class="post-metadata">

**Author:** ![daniel\_dev](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/daniel_dev/32/975_2.png) [@daniel\_dev](https://discuss.tryton.org/u/daniel_dev)\
**Post date:** [September 25, 2020, 12:33pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/7 "2020-09-25T12:33:13Z")

</div>

I tought about that…because instanciation of Party() is used when i create new records..doing a clean migration..

```
if Party.find(['code', '=', "C" + sanitize(df['ID_Tryton'][i])], limit=1):
    # p = Party()
    p, = Party.find(['code', '=', "C" + sanitize(df['ID_Tryton'][i])])
    if p:
        cm = p.contact_mechanisms.new()
        try:
            if sanitize_phone(df['TELF1'][i]).startswith('9'):
                if sanitize_phone(df['TELF1'][i]):
                    cm.value = sanitize_phone(df['TELF1'][i])
                    cm.type = 'phone'
            elif sanitize_phone(df['TELF1'][i]).startswith('6'):
                if sanitize_phone(df['TELF1'][i]):
                    cm.value = sanitize_phone(df['TELF1'][i])
                    cm.type = 'mobile'
            to_save.append(p)
            # print(cm.value, cm.type)
        except Exception as e:
            print(e)
            pass

```

Party.save(to\_save)

Replaced the search ID\_Tryton with Client\_ID and it worked 😌 🙃  
And this is just the start… Thank you for your patience ! Now all i have to do is validate the numbers with +34 and write the comments for each one 😃

---

<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:** [October 25, 2020, 12:33pm UTC](https://discuss.tryton.org/t/create-new-phones-over-existing-ones-for-each-party-contact-mechanism-migration/3273/8 "2020-10-25T12:33:19Z")

</div>

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