Home > database >  How to group data from a model with repeating names Python Odoo
How to group data from a model with repeating names Python Odoo

Time:05-13

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"  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

CodePudding user response:

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)
  • Related