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}"
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…
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)
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.