# Count number of records existing per customer(party)

**URL:** https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702
**Category:** Developer
**Created:** [November 14, 2023, 4:38pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702 "2023-11-14T16:38:45Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [November 14, 2023, 4:38pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/1 "2023-11-14T16:38:45Z")

</div>

Hi , I hope everyone here is fine

I need some help , I want to display the number of records registred per party based on the code of the party , Indeed i want everytime i want to create a new record, the field code display to me the number of records done by the party that i select when am trying to create the new record.

Here is my code but it doesn’t give me any result:

```auto
     party = fields.Many2One('party.party', 'Party')
     code = fields.Char("Code")
     @fields.depends('party')
     def on_change_with_code(cls, name=None):   
        counts = {}
        records = cls.search([])

        for record in records:
            code = record.party.code

            if code not in counts:
                counts[code] = 1
            else:
                counts[code] += 1

        for code, count in counts.items():
            return f"Code: {count}"

```

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: [November 14, 2023, 10:38pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/2 "2023-11-14T22:38:39Z")

</div>

> [@mimo\_Alva](#):
>
> I want to display the number of records

Where? because it looks like you want to display as a field on every record.

> [@mimo\_Alva](#):
>
> Indeed i want everytime i want to create a new record, the field code display to me the number of records done by the party that i select when am trying to create the new record.

I think you should describe first your actual goal instead of the solution you try to implement.

> [@mimo\_Alva](#):
>
> ```auto
> records = cls.search([])
> 
> for record in records:
> code = record.party.code
> 
> if code not in counts:
> counts[code] = 1
> else:
> counts[code] += 1
> 
> ```

It is probably better to count using a SQL query. It will be faster.

> [@mimo\_Alva](#):
>
> ```auto
> for code, count in counts.items():
> return f"Code: {count}"
> 
> ```

This loop stops after the first iteration. So I do not understand the goal.

---

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [November 15, 2023, 7:19am UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/3 "2023-11-15T07:19:28Z")

</div>

> [@ced](#):
>
> Where? because it looks like you want to display as a field on every record.

Hi, in the tryton interface, I have a custom module, there were a lot of customers where they have the access to create a new record where they want so the goal is to count the number of reords created per customer and display it the tryton interface as a field when he wants again to cretae new record, For example if **M.User1 try to create the new record and already have created 7 records** , it shows in the field **code: 7** when he want to create the new record , and if he did’nt yet create any record it will show **code: 1** , if M.user2 already has 3 records and he try to create new record it will shows in the field **code: 3** when he want to create the new record, etc…

---

<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: [November 15, 2023, 8:30am UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/4 "2023-11-15T08:30:12Z")

</div>

It looks like you want to display a `One2Many`. In list view the `One2Many` displays the number of record.

---

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [November 15, 2023, 12:19pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/5 "2023-11-15T12:19:11Z")

</div>

No a field type integer (it display per default number of records done per party(customer)) `party = fields.Many2One('party.party', 'Party')`, the issue that when u are on the way to create a new record and then choose the party , the number of records done per this party(customr) will be displayed on an integer `field code = fields.Char("Code")` that’s why i cant to use a change\_with function relay on the party code but am stuck on how to do that

---

<div class="post-metadata">

### Author: ![oscarQ](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/oscarq/32/413_2.png) [@oscarQ](https://discuss.tryton.org/u/oscarQ)
#### Post date: [November 15, 2023, 1:58pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/6 "2023-11-15T13:58:33Z")

</div>

I find this functionality a little weird, but if I understood you correctly and you really need it, I think this should work (I didn’t test it):

```auto
    @fields.depends('party')
    def on_change_with_code(self, name=None):
        if self.party:
          records = self.search([('party', '=', self.party.id)])
          return "Code: " + str(records.lenght() + 1)
        return ''

```

Anyway, consider doing this at least in the create method instead of in an on\_change or when changing record state (if the record has different states)

---

<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: [November 15, 2023, 2:31pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/7 "2023-11-15T14:31:32Z")

</div>

> [@oscarQ](#):
>
> ` records = self.search([('party', '=', self.party.id)])`

better to use the [count option](https://docs.tryton.org/projects/server/en/latest/ref/models.html#trytond.model.ModelStorage.search) as it should be faster.

```auto
count = self.search([('party', '=', self.party.id)], count=True)

```

But such function field can kill performance as it executes one SQL query per record read.  
So I still believe that a `One2Many` will fulfill the requirement.

---

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [November 15, 2023, 3:32pm UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/8 "2023-11-15T15:32:36Z")

</div>

thanks M.@ oscar your Code Suggestion works with me, I just modify `**str(records.lenght() + 1)---->str(len(records) + 1)**`

> [@ced](#):
>
> But such function field can kill performance as it executes one SQL query per record read.  
> So I still believe that a `One2Many` will fulfill the requirement.

thanks M.@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 16, 2023, 3:33am UTC](https://discuss.tryton.org/t/count-number-of-records-existing-per-customer-party/6702/9 "2023-11-16T03:33:36Z")

</div>

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