0
votes

The "normal way" of displaying data of a record would be: click the menu button, select a record in the tree view and you can see the form view for that specific record.

Since I only got one record containing some Highcharts graphs, I would like to skip the tree view and after clicking the menu button would like to see the form view of that one record, meaning the graphs.

I tried to only define the form view and no tree view but this just opens a new record instead of my existing one when clicking the menu button. I also searched for that problem but couldn't find anything helpful.

So any help is greatly welcome. Maybe there are even other ways of displaying the data/graphs without using the form view.

Here's the code so far:

menu.xml

<record model="ir.actions.act_window" id="statistics_action">
    <field name="name">Total Data</field>
    <field name="res_model">reminders.statistics</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="domain">[('id', '=', 1)]</field>
</record>

<menuitem id="statistics_group" name="Statistics" parent="reminder.main_reminder_menu"/>
<menuitem id="total_data_menu" name="My Graph" parent="reminder.statistics_group" action="statistics_action"/>

statistics_view.xml (the tree view I'd like to avoid/skip)

<record model="ir.ui.view" id="statistics_tree_view">
    <field name="name">reminder_statistics.tree</field>
    <field name="model">reminders.statistics</field>
    <field name="arch" type="xml">
        <tree edit="false" create="false" delete="false">
            <field name="id"/>
        </tree>
    </field>
</record>

<record model="ir.ui.view" id="statistics_form_view">
  <field name="name">reminder_statistics.form</field>
  <field name="model">reminders.statistics</field>
  <field name="arch" type="xml">
      <form edit="true" create="false" delete="false">
          <group string="Title of some Graph">
              <field name="some_graph" widget="charts_widget" nolabel="1" chart-type="stacked"/>
          </group>
      </form>
  </field>
</record>

Every day there is a cron job running executing this method to get the graphs data:

reminder_statistics.py

class Stats(models.Model):
    _name = 'reminders.statistics'

    some_graph = fields.Text()

    @api.one
    def collect_stats_data(self):
        data = some dictionary (may change every day)

        graph_data = {
            "title": {
                "text": ""
            },
            "xAxis": {
                "categories": [key for (key, value) in data.items()]
            },
            "yAxis": {
                "title": {
                    "text": "Total Count"
                }
            },
            "series": [{
                "type": "column",
                "name": "Total Count",
                "data": [value for (key, value) in data.items()],
                "dataLabels": {
                    "enabled": True,
                }
            },
            ],
            "tooltip": {
                "enabled": False
            }
        }

        self.some_graph = json.dumps(graph_data)
1

1 Answers

1
votes

You can pass the ID of the record to open with the action by setting the res_id field for the window action. However, this approach is brittle if the record does not exist.

A nicer way to do that would be to create the initial record at module installation and then pass the ID of that record to the window action using the record's XML ID, eg.

data.xml

<data noupdate="1">
    <record model="reminders.statistics" id="my_unique_record">
        <field name="some_graph">{}</field>
    </record>
</data>

menu.xml

<record model="ir.actions.act_window" id="statistics_ref_action">
    <field name="name">Total Data (With XML ID)</field>
    <field name="res_model">reminders.statistics</field>
    <field name="view_type">form</field>
    <field name="view_mode">form,tree</field>
    <field name="res_id" ref="reminder.my_unique_record"/>
</record>

<menuitem id="total_data_menu_with_xml_id" name="My Graph (Using XML ID)"
          parent="reminder.statistics_group" action="statistics_ref_action"/>

A yet more dynamic approach would be to use a server action and inside it search for some existing record (for example, the newest one) and return a window action displaying the found record:

menu.xml

<record model="ir.actions.server" id="statistics_server_action">
    <field name="name">Statistics Server Action</field>
    <field name="model_id" ref="model_reminders_statistics"/>
    <field name="state">code</field>
    <field name="code">
ids = self.search(cr, user.id, [], order='create_date desc', limit=1, context=context)
action = {
    "name": "Total Data (From Server Action)",
    "type": "ir.actions.act_window",
    "view_type": "form",
    "view_mode": "form,tree",
    "res_model": self._name,
    "res_id": ids and ids[0] or None,
}
    </field>
</record>

<menuitem id="total_data_menu_server_action" name="My Graph (Using a Server Action)"
          parent="reminder.statistics_group" action="statistics_server_action"/>