I am creating an addon in odoo to get some data and communicate it through a controller. I get the data I need from the DB correctly but when I transform it with json.dumps()
I get the following error:
TypeError: Object of type product.template is not JSON serializable - - -
So I decided to directly build a dictionary like this:
def cargarDiccionario(self, productos):
productos_dict = {
'skus': []
}
for producto in productos:
d = {
"name": producto.name,
"categ_id": producto.categ_id.name,
"booking_rom_num": producto.booking_rom_num,
"booking_floor": producto.booking_floor,
"booking_area": producto.booking_area,
"booking_lookout_area": producto.booking_lookout_area,
"is_booking_type": producto.is_booking_type,
"website_url": producto.website_url,
"booking_plan_ids": [x.id for x in producto.booking_plan_ids],
"product_template_image_ids": [
x.id for x in producto.product_template_image_ids],
"image_1024": self.imagen_procesar(producto.image_1024),
"description": producto.description,
"cost_currency_id": [x for x in producto.cost_currency_id],
}
productos_dict['skus'].append(d)
return productos_dict
Now I find that it producto.cost_currenci_id
contains elements that I don't know how they are named and I need to extract them, for example:
//Cost prodia contener
{
id:producto.cost_currency_id.id,
currenci:producto.cost_currenci_id.name,
price:producto.cost_currency_id.price,
}
How can I know the values that I eat producto.cost_currency_id
, .id
, .name
from .price
the console? And if possible, see the value with which they come. Similar to what happens with console.log(miObjeto)
in javascript.
Edit
I don't know how to access the model since the query is done like this:
from odoo import http
class OdooController(http.Controller):
@http.route("/barriotec/skus", auth="public", )
def index(self, **kw):
self.auth()
productos = http.request.env['product.template'].search([
["is_booking_type", "=", True],
# ["categ_id", "=", categoriaId],
])
self.auth(login=False)
datos = json.dumps(self.cargarDiccionario(productos))
return http.Response(
datos,
status=200,
content_type="application/json"
)
One way to do it, although there are surely better or perhaps more pythonic ones out there , would be to print the attributes of the object using the builtin method
dir()
. For example, the following function returns all the attributes of an object:However, this function not only shows the object's own attributes but also all those inherited, such as
__class__, __dict__, __hash__
and much more.One way to prevent it from displaying all Dunder methods is to apply a condition, for example, the following code does exactly that:
The result of the above code can be seen like this:
An improvement to the code shown above would be to inspect the
__dict__
object's attribute, which contains all the writable attributes (watch out for this) of the object.For example:
The result of this is the following:
As we can see, the attribute
is_good_programmer
is not shown in the__dict__
object's dictionary.We also have situations where the attribute
__dict__
is not present or is simply empty. For example:The result would be:
Knowing all this, we could write a method that takes this situation into account, returning both the modifiable attributes of the object and those that are not. Also, we can use the library
pprint
to display the output in a nicer way:The result should look like this:
With this you already have a very basic way of displaying the attributes of an object.
NOTE
Remember that in the last method we are showing the modifiable attributes and those that are not. It takes into account the issues of inheritance and static properties of a Class and how they are accessible only from the Class but not from the instance. You may be interested in only displaying the attributes listed on the object
__dict__
, if it is present.