# Generate a unique id when I import them

**URL:** https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932
**Category:** Developer
**Created:** [October 7, 2024, 1:10pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932 "2024-10-07T13:10:36Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)
#### Post date: [October 7, 2024, 1:10pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/1 "2024-10-07T13:10:37Z")

</div>

So, I want to import a file and generate a unique ID for each field during the import process. Is that possible? And if it’s possible. Where should I do this? Aside making sequential in my corresponding module.

---

<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 7, 2024, 2:54pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/2 "2024-10-07T14:54:42Z")

</div>

It is possible but you should not do anything to get it. The tryton framework does it automatically for you.

You just need to import the records. Once imported you can export the same record again. Make sure to include the `ID` field in the export which is the unique id generated by tryton.

---

<div class="post-metadata">

### Author: ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)
#### Post date: [October 7, 2024, 3:00pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/3 "2024-10-07T15:00:57Z")

</div>

Ah, no. I think this module make their own unique id.

```auto
    @classmethod
    def generate_puid(cls):
        STRSIZE = 8
        puid = ''
        for x in range(STRSIZE):
            if (x < 3 or x > 5):
                puid = puid + random.choice(string.ascii_uppercase)
            else:
                puid = puid + random.choice(string.digits)
        return puid

```

I had to adjust this to my company policy. So, yeah…

---

<div class="post-metadata">

### Author: ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)
#### Post date: [October 7, 2024, 5:14pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/4 "2024-10-07T17:14:50Z")

</div>

It might look like it will generate unique ids but this code is using randomness to generate the ids and if you’re not lucky two ids might collide.

It can work for your use case, but I would be wary of trusting all the ids to be unique.

You should use an `Unique` constraint so that you can rely on it.

---

<div class="post-metadata">

### Author: ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)
#### Post date: [October 8, 2024, 1:55am UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/5 "2024-10-08T01:55:11Z")

</div>

Ah, yes I am aware. I’ve changed it to a better sequence that will not have any redudancy. The thing is I have 900 records I suppose to insert to tryton. but i don’t know how to generate it if i have to import it.

---

<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 8, 2024, 5:54am UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/6 "2024-10-08T05:54:39Z")

</div>

You can look how it is done in [`ir.session` create method](https://foss.heptapod.net/tryton/tryton/-/blob/cbb381841768cf91419c43df93794c593d61ac8b/trytond/trytond/ir/session.py).

---

<div class="post-metadata">

### Author: ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)
#### Post date: [October 8, 2024, 12:13pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/7 "2024-10-08T12:13:56Z")

</div>

Can you elaborate, I am sorry I don’t understand.

---

<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 8, 2024, 2:26pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/8 "2024-10-08T14:26:26Z")

</div>

He means that there is an example on how to achieve what you want to do in the create method of ir.session model.

I copy here the relevant functions:

```python

    @classmethod
    def default_key(cls, nbytes=None):
        return token_hex(nbytes)

    @classmethod
    def create(cls, vlist):
        vlist = [v.copy() for v in vlist]
        for values in vlist:
            # Ensure to get a different key for each record
            # default methods are called only once
            values.setdefault('key', cls.default_key())
        return super().create(vlist)

```

Here is a brief explanation of what does each function:

- `default_key`: Generates a secure random hexadecimal string, potentially used as a unique identifier for a record.
- `create`: Customizes the record creation process by ensuring that each record gets a unique key (using `default_key`) if one is not provided in the input data. Then, it proceeds to create the records via the parent class’s `create` method.

---

<div class="post-metadata">

### Author: ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)
#### Post date: [October 8, 2024, 8:21pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/9 "2024-10-08T20:21:22Z")

</div>

Ah, so if I import them through tryton I should write that in my module?

---

<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 9, 2024, 8:01am UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/10 "2024-10-09T08:01:47Z")

</div>

Yes, you should write that on your module, activate it on the database and then the server will execute the custom code to generate the unique id whenever a new record is created (thanks to the fact that you overrided the `create` function)

---

<div class="post-metadata">

### Author: ![Siobhan](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Siobhan](https://discuss.tryton.org/u/Siobhan)
#### Post date: [October 9, 2024, 2:23pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/11 "2024-10-09T14:23:11Z")

</div>

Ah, okay, thank you, I understand.

---

<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 10, 2024, 2:24pm UTC](https://discuss.tryton.org/t/generate-a-unique-id-when-i-import-them/7932/12 "2024-10-10T14:24:01Z")

</div>

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