Reputation: 181
I'm stuck with a problem on Odoo17. I want to use the wizard on a project to display the main informations as the user_id or the order_lines that the project depends on. So to do that, maybe it's not the best way, I created a wizard (it works well), in the wizard view I added field that a javascript widget controls. That javascript call a with fetch controller that send back JSON data (it works well) to the javascript. Finally I display those data in a Qweb template. And here starts my problem : I can read some simple values but it cant read objects, don't know why. Here is my code :
controllers/project_infos.py
# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request
from itertools import groupby
import json
from operator import itemgetter
class MyWebController(http.Controller):
@http.route('/addon/projet', auth='user', csrf=False )
def my_controller_method(self, **kwargs):
id = kwargs['id']
project =request.env['project.project'].search([('id', '=', id)])
clientId = project.partner_id
client = request.env['res.partner'].search([('id', '=', clientId.id)])
clientName = client.name
clientId = project.partner_id
managerId = project.user_id
saleId= project.sale_order_id
projectTasks = request.env['project.task'].search([('project_id', '=',project.id), ('name', '=','Mise en facturation Complète')])
manager = request.env['res.users'].search([('id', '=', managerId.id)])
managerName = manager.name
client = request.env['res.partner'].search([('id', '=', clientId.id)])
clientName = client.name
clientStreet = client.street
clientStreet2 = client.street2
clientZip= client.zip
clientCity = client.city
commande = request.env['sale.order'].search([('id', '=', saleId.id)])
order_lines = request.env['sale.order.line'].search([('order_id','=',commande.id)])
arr = []
for line in order_lines :
print (line.product_uom_qty)
product = request.env['product.template'].search([('id', '=', line.product_template_id.id)])
productCategory = str(product.categ_id.name)
productDescription = str(product.description_sale)
print ()
if productCategory != 'False':
arr.append({"name":line.product_template_id.name, "desc":productDescription, 'cat':productCategory, 'qty':line.product_uom_qty})
d = sorted(arr, key=itemgetter("cat"))
grouped = {k: list(v) for k, v in groupby(d, itemgetter('cat'))}
val = {
'manager' : managerName,
'name': clientName,
'street': clientStreet,
'street2': clientStreet2,
'city' : clientCity,
'zip': clientZip,
'products':grouped
}
return json.dumps(val)
static/src/js/edit.js
/** @odoo-module */
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { registry } from "@web/core/registry";
const { Component, onWillStart, useState } = owl
export class Edit extends Component {
setup() {
this.state = useState({
check: true
})
onWillStart(async () => {
await this.projetInfos()
})
}
async projetInfos() {
var projectId = this.env.model.root.evalContext.context.id
fetch('/addon/projet?id=' + projectId)
.then(response => response.json())
.then((data) => {
var Format = []
const result = (data['products'])
Object.keys(result).map((d, index) => {
Format.push({ id: index, category: d, products: data['products'][d] })
})
//a simple string
this.state.manager = data['manager']
//an array of object
this.state.products = Format
})
}
}
Edit.template = "addon.edit";
Edit.props = {
...standardFieldProps,
};
Edit.supportedTypes = ["char"];
export const activeField = { component: Edit };
registry.category("fields").add("edit", activeField);
So everything is ok till then : I got all the data I want but when I want to display the state product which are nested objects Qweb tells me it's undefined.
static/src/xml/edit.xml
<?xml version="1.0" encoding="UTF-8"?>
<templates id="chart" xml:space="preserve">
<t t-name="addon.edit" owl="1">
<!-- ✅ Display the name of Manager -->
<span>Manager :</span> <p t-esc='state.manager' />
<!-- ❌ OwlError: Invalid loop expression: "undefined" is not iterable -->
<t t-foreach="state.products" t-as="product" t-key="product.id">
<p t-esc="product.category" />
</t>
</t>
</templates>
So I really don't know why I got this error. When I console.log the Format array, copy/paste it somewhere outside the fetch call in a state of my component, the Qweb loop works fine.
Somebody could help me ?
Thanks a lot in advance
Upvotes: 0
Views: 372