'How to group data from a model with repeating names Python Odoo

My Odoo model has repeating values. How to group data from a model with repeating names. I need to get a data format like this

Administrator : name Administrator age 33  
                name Administrator age 43
                name Administrator age 32
Sam: name Sam age 31
     name Sam age 22
     name Sam age 32
     name Sam age 12
Anastatia : name Anastasia age 44
            name Anastasia age 55
            ...

.py

class FirstTestModel(models.Model):
    _name = "first.model"
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = "Test Model"

    name = fields.Char(string='Name', required=True, )
    age = fields.Integer(string='Age', )

    def action_calculate(self):
        search = self.search([])
        for all_values in search:
            string_name_age = "%s : %s" % (all_values.name, str(all_values.age))
            print(string_name_age)

List of users in the database

enter image description here

When I go to the user page I have a calculate button. When I click it I get the Python terminal data

.xml

<record id="first_form" model="ir.ui.view">
        <field name="name">first.model.form</field>
        <field name="model">first.model</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button id="button_calculate" name="action_calculate" string="Calculate" class="btn-primary" type="object"/>

enter image description here

The point is to get all the same names from the database, sorted by age, and put them into groups. For example, all Administrator names must be a group.

 Administrator : name Administrator age 35
             name Administrator age 37
             name Administrator age 56
             name Administrator age 66
Sam : name Sam age 13
      name Sam age 22
      name Sam age 32
      name Sam age 55
      name Sam age 62

and so on.

Then I would transfer this data to the page in Odoo I just started learning Python and odoo



Solution 1:[1]

Below code might give a similar output, but why you need it is what makes me curious. Please let us know what you are trying to achieve so we can help better

def action_calculate(self):
    search = self.search([])
    string_name_age = ""
    for all_values in search:
        same_name_items = self.search([("name","=",all_values.name)], order="age asc")
        string_name_age += all_values.name + " :"
        for items in same_name_items:
            string_name_age += "name: %s  age %s \n" % (items.name, str(items.age))
    print(string_name_age)

Solution 2:[2]

I changed the code so that there would not be so many requests to the database, through the filtered method.

My code :

def action_calculate(self):
    search = self.search([])
    string_name_age = ""
    name_done = []
    for all_values in search:
        if all_values.name not in name_done:
            same_name_items = search.filtered(lambda lm: lm.name == all_values.name).sorted(key=lambda r: r.age)
            string_name_age += all_values.name + " :"
            for items in same_name_items:
                string_name_age += "name: %s  age %s \n" % (items.name, str(items.age))
            name_done.append(all_values.name)

    print(string_name_age)
    self.message_post(body=string_name_age)

This is working.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Anish B.