Count number of records existing per customer(party)

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:

     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

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

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

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

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

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…

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

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

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):

    @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)

1 Like

better to use the count option as it should be faster.

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.

1 Like

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

thanks M.@ced

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