Help

Welcome!

This community is for professionals and enthusiasts of our products and services.
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

0

How to set computed one2many field in Odoo

Avatar
Balaraman
0
1
9,434

I just want to know how to set the values in one2many by using computed field calculation. Here my code is

Python file

from openerp import models ,fields,api
from openerp import SUPERUSER_ID
from dateutil.relativedelta import relativedelta
import openerp.addons.decimal_precision as dp
import math
import logging
import datetime

class extend_product_product(models.Model):
    _inherit = 'product.product'

    monthly_lines = fields.One2many('minmax.monthly.data','month_id', 'Monthy Sales',compute="_get_monthly_sales")

 @api.one
 def _get_monthly_sales(self):
    vals = {}
    vals.update({'monthly_lines':[(0,0,{'month_name_id':1,'so_qty':35})]})
    self.write(vals) # Also I have tried self.monthly_lines = [(0,0,{'month_name_id':1,'so_qty':35})]

class minmax_monthly_data(models.Model):
    _name = 'minmax.monthly.data'

    month_id = fields.Many2one('product.product', 'Product Reference', select=True, required=True)
    month_name_id = fields.Many2one('minmax.months','Minmax Month',required=True)
    so_qty = fields.Float('Sales Qty', digits_compute=dp.get_precision('Product Unit of Measure'), required=True)


XML File
<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
      <record model="ir.ui.view" id="product_monthly_minmax_tree">
            <field name="name">product.product.tree</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="product.product_product_tree_view"/>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <field name="ean13" position="after">
                    <field name="monthly_lines" widget="one2many_tags" />
                </field>
            </field>
        </record>
    </data>
</openerp>


Here I have tried to insert the data manually. The function is called properly whenever we load the product.product tree view. But there is no results. Thanks in advance.
Avatar
Balaraman
Avatar
Discard
1 Answer
0
Best Answer

I got your idea. You want to have report on monthly sales report of each and every report.
Actually, you want to CREATE (instead of UPDATE) minmax.monthly.data each time you load your product.product. So, instead of self.write, you must do minmax_monthly_data.create(vals)

Anyway, your design is bad. Odoo already has sales report to show you that kind of analysis.

Comment Share
1 Comment
Avatar
Discard
Avatar
ViindooBot
-

Thanks for your reply. I am creating the report in php to interconnect odoo using XML-RPC. If I am using the sales report then I need to call sales.report model for every product and every month in the db. For this complexity I am going to stored the sales details in monthly wise by one2many field. So I can get the details from minmax.monthly.data. The report is hold the months as column and products are comes to the rows. So I need to store those details in that computed one2many field. Any idea?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Q&A Forum Regulations

Forum Regulations

By using the FAQ Forum on ERPOnline.vn website, you unconditionally agree to the following terms:

    1. Q&A Forum is a discussion environment to support each other in Odoo/ERPOnline implementation, in the spirit of "community support community", with internet address at: https://www.erponline.vn/forum/hoi-dap-7

    2. You are thoroughly responsible for the content submitted by you.

    3. DO NOT use the Q&A Forum for any activities that violate Vietnamese law and International Conventions to which Vietnam is a participant.

    4. DO NOT post content that is offensive, defamatory, or discredits other individuals or legal entities.

    5. DO NOT post advertisements or advertising content.

    6. DO NOT post content that is not related to management software and ERP in general as well as OpenERP and ERPOnline.vn in particular.

    7. DO NOT post copyrighted content or content, materials, products protected by Intellectual Property law without the written consent of the author.

    8. DO NOT embed images from other websites.

    9. You agree that all options, solutions, answers in the Q&A section on ERPOnline.vn website are for reference only. ERPOnline.vn website will not be liable if you have any damage when applying the plans given in the Q&A section on ERPOnline.vn website.

These terms and conditions may be amended to suit reality at any time without prior notice.