# Automatically incrementing number in target model

**URL:** https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507
**Category:** Developer
**Created:** [June 26, 2019, 4:34pm UTC](https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507 "2019-06-26T16:34:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [June 26, 2019, 4:34pm UTC](https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507/1 "2019-06-26T16:34:12Z")

</div>

There is a list of employees in our application.  
Each employee has a list of records with a unique number within this list.  
When adding a new record for an employee, the number is automatically incremented.

```
class Empl(ModelSQL, ModelView):
    "Empl"
    __name__ = 'module.empl'
    .....
    list = fields.One2Many('module.list', 'empl_id', 'List')

class List(ModelSQL, ModelView):
    "List"
    __name__ = 'module.list'
    .....
    empl_id = fields.Many2One('module.empl', 'Empl_id')
    number = fields.Numeric('Number')

    @classmethod
    def default_number(cls):
        employee_id = ?????
        lines = cls.search(
            [('empl_id', '=', employee_id),
            ], order=[('number', 'DESC')], limit=1)
        if lines:
            number = lines[0].number + 1
        else:
            number = 1
        return number

```

Is it possible to get `id` of the current employee (`employee_id`) in the method `default_number`?

---

<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: [June 26, 2019, 5:19pm UTC](https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507/2 "2019-06-26T17:19:13Z")

</div>

> [@bell](#):
>
> Is it possible to get `id` of the current employee ( `employee_id` ) in the method `default_number` ?

No you can not have it. Default methods are called before any field are filled.  
But you can update the number field using an on\_change on the empl\_id (strange to suffix with id).  
Also be careful that your code does not work on concurrent environment and neither if you add more than 1 line at a time.  
If you just want to have an order, you should use the `sequence_ordered` mixin and activate the `sequence` on the tree view.  
If you want to have sequential numbering, you may use an `ir.sequence` and fill the field on `create` like the sale. But in your case you will need to create a sequence for each employee.

---

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [June 27, 2019, 4:29pm UTC](https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507/3 "2019-06-27T16:29:45Z")

</div>

It seems too expensive to create a sequence for each employee.  
So we will update the number field using the method `on_change`.  
In addition, our users can edit the number in the employee list.  
In concurrent environment they may receive an error message about the number uniqueness.

Thanks Cedric for the help and detailed answer.

---

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [July 4, 2019, 1:54pm UTC](https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507/4 "2019-07-04T13:54:35Z")

</div>

We have found a better solution to our problem.  
We used `on_change_list` method of `Empl` class instead of `on_change_empl_id` method of `List` class:

```
@fields.depends('list')
def on_change_list(self):
    if self.list[-1].number == 0:
        max_number = max([x.number for x in self.list])
        self.list[-1].number = max_number + 1

```

And there is `default_number` method in `List` class:

```
@staticmethod
def default_number():
    return 0

```

Of course this solution does not work on concurrent environment, but it works if we add more than 1 line at a time.

---

<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: [August 3, 2019, 2:00pm UTC](https://discuss.tryton.org/t/automatically-incrementing-number-in-target-model/1507/5 "2019-08-03T14:00:03Z")

</div>

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