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

How to override def in python ?

By
Silviaa
on 9/2/16, 6:33 AM 395 views

My Original code in sale_analytic.py

class AccountAnalyticLine(models.Model):

_inherit = "account.analytic.line"

@api.multi

def write(self, values):

if self._context.get('create', False):

return super(AccountAnalyticLine, self).write(values)

todo = self.mapped('so_line')

result = super(AccountAnalyticLine, self).write(values)

if 'so_line' in values:

todo |= self.mapped('so_line')

for line in self:

res = self._get_sale_order_line(vals=values)

super(AccountAnalyticLine, line).write(res)

if 'so_line' in res:

todo |= line.mapped('so_line')

todo._compute_analytic()

return result

Here I have an new file named sample.py

I have inherited same class.

class AccountAnalyticLine(models.Model):

_inherit = "account.analytic.line"

@api.multi

def write(self, values):

if self._context.get('create', False):

return super(AccountAnalyticLine, self).write(values)

todo = self.mapped('so_line')

result = super(AccountAnalyticLine, self).write(values)

if 'so_line' in values:

todo |= self.mapped('so_line')

for line in self:

res = line._get_sale_order_line(vals=values)

super(AccountAnalyticLine, line).write(res)

if 'so_line' in res:

todo |= line.mapped('so_line')

todo._compute_analytic()

return result

Instead of this line "res = self._get_sale_order_line(vals=values)" I need to change it as

"res = line._get_sale_order_line(vals=values)"

Can anyone please help me?

1
Premananth
On 9/4/16, 5:27 AM

res = line.sudo()._get_sale_order_line(vals=values)

-1

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

Techno-Functional Associate with 6+ years of experience in Odoo (formerly known as OpenERP).

deep
On 10/26/16, 2:25 AM

I believe, for loop is not need,

But if you insist upon have it, no issues, However the called function api decorator should match it.

If you want to call the method _get_sale_order_line, for each and every record,  ensure you have added decorator as api.one, that way you can make a call individually for each record in the For Loop

@api.one
def _get_sale_order_line(self, vals)

in source obj

for line in self:
    res = line._get_sale_order_line(vals=values)

Else keep it has api.multi, and make a call without the help of For Loop.

@api.multi
def _get_sale_order_line(self, vals)

 accordingly in source obj:

res = self._get_sale_order_line(vals=values)

Note:

you are performing your action in the ORM Write method, of type multi, however be aware, not always self._ids will be array/multiple, chances are there it can be singleton record/id. Hence Ensure before proceeding with you action.

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

1 follower(s)

Stats

Asked: 9/2/16, 6:33 AM
Seen: 395 times
Last updated: 10/26/16, 2:25 AM