user280960
user280960

Reputation: 721

odoo report print object value

I am new to odoo and learning a bit related to reporting. i was looking around stock picking report. i noticed that it has an object called "docs" and the values are stored inside this object. as i am from PHP background there are possibilities to print the object received for debugging purpose as follows:

<?php print_r("object"); ?>

So i tried to print the same thing in odoo using QWeb template using below codes :

1. <t t-esc="docs" />
2. <t t-foreach="docs" t-as="o">
      <t t-esc="o" />
3. <t t-foreach="docs" t-as="o">
      <t t-esc="o_all" />
4. <t t-foreach="docs" t-as="o">
      <t t-esc="o_value" />
5. <t t-foreach="docs" t-as="o">
      <t t-esc="o_all" />

But i was unable to print the whole object, i only get stock.picking(1,) Can someone help me how can i see the whole object with keys and values in qweb template. Moreover can someone guide me where this object "docs" is defined.

I will really appreciate.

Thanks,

Upvotes: 3

Views: 4198

Answers (3)

Than
Than

Reputation: 446

Based on @Diderh answer, see how to display each attributes of an object with their related values:

<table>
    <t t-foreach="product._fields" t-as="field">
        <tr>
            <td>
                <t t-esc="field" />
            </td>
            <td>
                <t t-esc="product[field]" />
            </td>
        </tr>
    </t>
</table>

Upvotes: 3

Didierh
Didierh

Reputation: 374

Sometime we are a little lost how to print the fields for an particular object in website Odoo v12, you can use a simple code to show wholes fields. Here how i did it for Object Forum:

<t t-foreach="forums.sorted(reverse=True)" t-as="forum">
   <t t-esc="forum._fields" />
</t>

Upvotes: 1

danidee
danidee

Reputation: 9624

stock.picking(1,) is a single object so it doesn't have key and values which dictionaries have. so i guess what you want to see are the attributes the object has like the id, name and so on

you can do that by using the dir function, that's the equivalent of print_r on an object in php (not really),

<t t-foreach="docs" t-as="o">
    <t t-esc="dir(o)" />

That will print out all the attribute that the object has, if you want to see a specific attribute like the id you can do this

<t t-foreach="docs" t-as="o">
    <t t-esc="o.id" />

dir gives a lot more information, but you can still get the same behaviour as php's print_r with some little modifications. for example

class Example:
    whatever = 'whatever attribute'
    something = 'something attribute'

ex = Example()

print({attribute: getattr(ex, attribute) for attribute in dir(ex) if not attribute.startswith('__')})

Here i used a dictionary comprehension to loop through all the attributes of the object returned by dir before then i used an if statement to strip out all the extra information about the object that dir gives us which usually start with two underscores (__).

Hence the output is a dictionary with the object attributes as keys and the values are the contents of those attributes

{'whatever': 'whatever attribute', 'something': 'something attribute'}

Untested by me. but this should work in QWeb also

<t t-foreach="docs" t-as="o">
    <t t-esc="{attribute: getattr(o, attribute) for attribute in dir(o) if not attribute.startswith('__')}" />

Upvotes: 1

Related Questions