Skip to Content
Menu
This question has been flagged
2 Replies
2538 Views

how i can show or print value of a field or variable to verify, i am adding Compute method on field of a model through Technical -> Models ?

i tried print() and raise userError but not working. for print() it is throwing error, said 'print' is undefined.

please guide

regards

Avatar
Discard
Author

any hope???

Author

any hope of an Answer?

Best Answer

You cant send the info from the frontend to backend, so if you require it in logging file of Odoo that's not possible.

You could create a server action:


raise UserError(str(record.x_field_name))


1. Add server action to action button

2. Open de record where you want to see the value of the field

3. Click on 'Action > Action Name'


A pop-up will be shown with the value of the compute result.

Avatar
Discard
Author

i just want to debug code and see the values in log file. is it possible or not while adding field through Technical -> Models ?

if yes, then how... a very simple question.

regards

Author

thank you @Dennis Ochse for your interest to help us. will check this.

Best Answer

Hi,

to add a compute field under a model, try this code, for your reference


from odoo import api, fields, models


class YourModel(models.Model):

    _name = 'your.model'


    field1 = fields.Float(string='Field 1')

    field2 = fields.Float(string='Field 2')


    computed_field = fields.Float(string='Computed Field', compute='_compute_computed_field', store=True)


    @api.depends('field1', 'field2')

    def _compute_computed_field(self):

        for record in self:

            # Your computation logic here

            result = record.field1 + record.field2


            # Set the value of the computed field

            record.computed_field = result


when we apply the 'store=True', that value will be stored in the database, and add the field in the form view or list view of the model you can see the field value, If the field value is not displayed you can use the 'force_save="1"' attribute ude the xml code


xml code here:



<odoo>

    <data>


        <!-- Tree View -->

        <record id="view_your_model_tree" model="ir.ui.view">

            <field name="name">your.model.tree</field>

            <field name="model">your.model</field>

            <field name="arch" type="xml">

                <tree>

                    <field name="field1"/>

                    <field name="field2"/>

                    <field name="computed_field" force_save="1"/>

                </tree>

            </field>

        </record>


        <!-- Form View -->

        <record id="view_your_model_form" model="ir.ui.view">

            <field name="name">your.model.form</field>

            <field name="model">your.model</field>

            <field name="arch" type="xml">

                <form>

                    <group>

                        <field name="field1"/>

                        <field name="field2"/>

                        <field name="computed_field" readonly="1"/>

                    </group>

                </form>

            </field>

        </record>


        <!-- Menu Action -->

        <record id="action_your_model" model="ir.actions.act_window">

            <field name="name">Your Model</field>

            <field name="res_model">your.model</field>

            <field name="view_mode">tree,form</field>

        </record>


        <!-- Menu Item -->

        <menuitem id="menu_your_model" name="Your Model" parent="base.menu_sales" action="action_your_model"/>


    </data>

</odoo>




Regards

Avatar
Discard
Author

omg.... i think you failed to understand what i asked for. in a computed field which i created through Technical, in compute method how i can print / show the value of a field or a variable in log file.

hope this clear now.

Related Posts Replies Views Activity
3
Feb 24
2968
2
Jan 24
5169
2
Sep 24
1688
0
Jan 24
1266
3
May 23
16467