# How to add owner's bank account ? ( Migration )

**URL:** <https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347>\
**Category:** Developer\
**Tags:** bank\
**Created:** [October 13, 2020, 5:41pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347 "2020-10-13T17:41:51Z")\
**Posts on this page:** 13\
**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:** [October 13, 2020, 5:41pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/1 "2020-10-13T17:41:51Z")

</div>

Hi. I would like to know, how can i add the owner in the model Bank\Bank accounts ?  
I did do somthing like  
ba = BankAccounts()  
add\_owner = ba.owners.new()  
add\_owner = Party.find([])[0]  
…but that did not work , or:  
`add_owner.name ... or add_owner.code..` not at all. I noticed that it haves a strange label and ± signs on the right but i can’t figure it out how should i call it internally to trigger the options and send the party i need to bind with the right acc number… Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 13, 2020, 6:00pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/2 "2020-10-13T18:00:32Z")

</div>

The `owners` field is a `Many2Many` field. That means you have to fill it with a list of integers. Using Proteus you can do something like (I’m doing this off the top of my head, so anybody correct me if I’m wrong):

```auto
BankAccount = Model.get('bank.account')
Party = Model.get('party.party')

parties = Party.find([])

ba = BankAccount()
ba.owner = [p.id for p in parties]
ba.save()

```

Also see [Tryton Scripting Client — Library to access Tryton server as a client](https://docs.tryton.org/projects/client-library/en/latest/) and in particular how you can add `categories`. Just to mention, this is a very simple and probably won’t work because you have to fill more fields with data.

---

<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:** [October 13, 2020, 7:49pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/3 "2020-10-13T19:49:14Z")

</div>

```
to_save = []    
df = pandas.read_excel('banks.xlsx', dtype="str", engine="openpyxl")
    for i in range(len(df)):
        if sanitize(df['Name'][i]) and sanitize(df['Bankname'][i]) and sanitize(df['BIC'][i]):
             ba = BankAccount()
             ba.bank = Bank.find(['bic', '=', sanitize(df['BIC'][i])])[0]
             ba.currency = Currency.find(['code', '=', 'EUR'])[0]
             an = Party.find(['code', '=', 'C' + sanitize(df['Code'][i])], limit=1)
             ba_new_owner = ba.owners.new()
             ba_new_owner = [p.id for p in an][0] # passes as int
             ba_number = ba.numbers.new()
             ba_number.number = 'ES91 2100 0418 4502 0005 1332' # TEST IBAN ( valid Iban )
             num.type = 'other' # switch to 'iban' after validation and test.
             to_save.append(ba)
BankAccount.save(to_save)

```

 ![bank owner not saved](https://discuss-cdn.tryton.org/uploads/default/original/2X/0/0de8769ed205aa21608f1804aa193a0372a029a5.png)

First, thank you @edbo for your reply ! 🙂 I’ve tryed this..but still, it goes only with the code..and not selecting the first party witch i search by existing code, both in xlsx file and tryton… 🤔

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 13, 2020, 8:18pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/4 "2020-10-13T20:18:34Z")

</div>

I find your code weird … 😕 I don’t get the `ban = ba.owners.new()` or the `num = ba.numbers.new()`. Again please take a look at the link I provided. You can get a clear picture out of it how to add data to the `owners` and `number` fields.

Just a wild guess: `ba.owners = an` or `ba.owners.extend(an)`

---

<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:** [October 13, 2020, 9:13pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/5 "2020-10-13T21:13:22Z")

</div>

Thank you for your repIy ! I have made my code more clear. It really douse not matter how i name my variables.. what i care more is to find out more methods, and the why’s behind, and also leave them here for further learning for someone else. The fact that i am not using the docs …is that i didn’t get it from the docs. Before asking for help i google, i read code, i try things and if none works.. i get here and ask. So… if i use those .new() methods with fields numbers and owners it’s because it works…do not ask me why, because i didn’t get an explanaition from nobody, but it works for some cases. In the second case when adding the account numbers, works like magic. In the first case it dousen’t. If any can do explain as for starters i will apreciate it and many more readers after, i bet! 😃

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 13, 2020, 9:40pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/6 "2020-10-13T21:40:44Z")

</div>

```auto
to_save = []
df = pandas.read_excel('banks.xlsx', dtype="str", engine="openpyxl")
    for i in range(len(df)):
        if sanitize(df['Name'][i]) and sanitize(df['Bankname'][i]) and sanitize(df['BIC'][i]):
             ba = BankAccount()
             ba.bank = Bank.find(['bic', '=', sanitize(df['BIC'][i])])[0]
             ba.currency = Currency.find(['code', '=', 'EUR'])[0]
             own = Party.find(['code', '=', 'C' + sanitize(df['Code'][i])], limit=1)
             ba.owner = own
             ba_number = ba.numbers.new()
             ba_number.number = 'ES91 2100 0418 4502 0005 1332' # TEST IBAN ( valid Iban )
             ba_number.type = 'other' # switch to 'iban' after validation and test.
             to_save.append(ba)
BankAccount.save(to_save)

```

Play with the `ba.owner = own` to find the right one. It should be a `List`.

---

<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:** [October 13, 2020, 10:05pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/7 "2020-10-13T22:05:57Z")

</div>

> [@edbo](#):
>
> ` ba.owner = own`

A bank account does not have an `owner` but a list of `owners` so the code should be:

```auto
ba.owners = [own]

```

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [October 13, 2020, 10:23pm UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/8 "2020-10-13T22:23:05Z")

</div>

Ahh typos ☹ But the `Party.find()` is returning a `List` right? Even if it has a `limit=1`.

---

<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:** [October 14, 2020, 7:04am UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/9 "2020-10-14T07:04:44Z")

</div>

> [@edbo](#):
>
> But the `Party.find()` is returning a `List` right? Even if it has a `limit=1` .

Yes it does so it should be:

```auto
own, = Party.find(['code', '=', 'C' + sanitize(df['Code'][i])], limit=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:** [October 14, 2020, 7:34am UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/10 "2020-10-14T07:34:29Z")

</div>

Hey guys! (@ced. @edbo ) Thank you for your patience. I have did the part of :

```
own, = Party.find(['code', '=', 'C' + sanitize(df['Codigo'][i])], limit=1)

```

it returns a class instance from what i know.. and after

`ba.owners = [own]` witch converts it in to a list like this [proteus.Model.get(‘party.party’)(173)]

```
but that ...leaves to ".../lib/python3.6/site-packages/proteus/ __init__.py", line 287, in __set__
    raise AttributeError
AttributeError

```

and if i try using the .new() method when i want to save a new owner, it saves only the code just as in the picture i’ve left before with the result. Really lost here.. there is maybe another way to deal with this ? Or is it me that i do not understad completely how it works ? First time i did run it from the party.party side..but when i wanted to save the account bank numbers i could’nt..so i checked the bank.account and seemed that i can choose the owner directly from here, but still..can’t figure it out why dousent takes the owner as i am doing manually.

 ![saved owner](https://discuss-cdn.tryton.org/uploads/default/original/2X/8/8fc8249ebb32bf819d53930c90d4a889f0187d42.png)

---

<div class="post-metadata">

**Author:** ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)\
**Post date:** [October 14, 2020, 7:45am UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/11 "2020-10-14T07:45:49Z")

</div>

> [@daniel\_dev](#):
>
> `ba.owners = [own]` witch converts it in to a list like this [proteus.Model.get(‘party.party’)(173)]

In proteus it is not possible to assing values on xxx2many fields. You should use append to add a single value or extend to include a list.

So this should work for you:

```python
ba.owners.append(own)

```

Hope it helps!

---

<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:** [October 14, 2020, 7:50am UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/12 "2020-10-14T07:50:52Z")

</div>

Thank you @pokoli! This is what i’ve been looking for! i did’nt even think of that ! 😓 👏 👏  
Also, a big thanks for @edbo and @ced 🙂

---

<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:** [November 13, 2020, 7:50am UTC](https://discuss.tryton.org/t/how-to-add-owners-bank-account-migration/3347/13 "2020-11-13T07:50:55Z")

</div>

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