Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Reading Parent/Child For

By
Alexandre Pezzini
on 8/26/14, 8:28 AM 977 views

I'm new in Odoo and Phyton. I've just develop a module to extend CRM, adding products in opportunities.

I used function fields to calculate price vs. quantity on opportunities lines (products). Now I want to sumarize all lines and put on opportunity's planned_revenue field. But I don't know what to put in this FOR statment. This line rises an error:  for line in x_crm_lead.crm_lead_line: "crm_lead.py", line 13, in _amount_all AttributeError: type object 'x_crm_lead' has no attribute 'crm_lead_line'

There is my two classes:

class x_crm_lead(osv.Model):
    _name = 'crm.lead'
    _description = 'CRM Lead Extended'
    _inherit = 'crm.lead'

    def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for lead in self.browse(cr, uid, ids, context=context):
            res[lead.id] = {}
            val = 0.0
            for line in x_crm_lead.crm_lead_line:
                val += line.price_subtotal
            res[line.id]= val
        return res

    _columns = {
        'planned_revenue': fields.function(_amount_all,string='Receita Esperada',type='float',store=True),
        'crm_lead_line': fields.one2many('crm.lead.line','crm_lead_id','Produto da Oportunidade')
    }

class crm_lead_line(osv.Model):
    _name = 'crm.lead.line'
    _description = 'CRM Lead Line'

    def _amount_line(self, cr, uid, ids, name, arg, context=None):
        res = {}
        for record in self.browse(cr, uid, ids, context):
            res[record.id] = float(record.price_unit) * float(record.quantity)
        return res


    _columns = {
        'crm_lead_id': fields.many2one('crm.lead','Oportunidade',ondelete='cascade'),
        'product_id': fields.many2one('product.product','Produto'),
        'description': fields.text('Descricao'),
        'quantity': fields.float('Quantidade',digits=(12,2)),
        'price_unit': fields.float('Preco Unitario',digits=(12,2)),
        'price_subtotal': fields.function(_amount_line,string='Subtotal',type='float',store=True),
    }

Thank you in advance!

0

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 8/26/14, 8:42 AM

Try this method:

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for lead in self.browse(cr, uid, ids, context=context):
            val = 0.0
            for line in lead.crm_lead_line:
                val += line.price_subtotal
            res[lead.id]= val
        return res

The outer loop iterates over all selected lead records, while the inner loop iterates over all lines for each lead.

Also think about renaming your filed in crm_lead_line_ids. This is more intuitive.

EDIT:
If you change the field name, then you have to change the code accordingly:

for line in lead.crm_lead_line:

to:

for line in lead.crm_lead_line_ids:

 

Hope it helps.

Regards.

Hi René, thank for help. I made the changes you suggested: class x_crm_lead(osv.Model): _name = 'crm.lead' _description = 'CRM Lead Extended' _inherit = 'crm.lead' def _amount_all(self, cr, uid, ids, field_name, arg, context=None): res = {} for lead in self.browse(cr, uid, ids, context=context): val = 0.0 for line in lead.crm_lead_line: val += line.price_subtotal res[lead.id]= val return res _columns = { 'planned_revenue': fields.function(_amount_all,string='Receita Esperada',type='float',store=True), 'crm_lead_line_ids': fields.one2many('crm.lead.line','crm_lead_id','Produto da Oportunidade') } But still rises an error: "crm_lead.py", line 13, in _amount_all AttributeError: type object 'crm_lead_line' has no attribute 'crm_lead_id' What is wrong here?

Alexandre Pezzini
on 8/26/14, 10:00 AM

Edited my answer.

René Schuster
on 8/27/14, 3:35 AM

Thank you René! The error was occurring because of a missing coma, at second column of x_crm_lead class.

Alexandre Pezzini
on 8/28/14, 3:47 PM

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!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 8/26/14, 8:28 AM
Seen: 977 times
Last updated: 3/16/15, 8:10 AM