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

Can I browse an object on a function defined on another object ? v7 [Functionnal Field]

By
Yassine TEIMI
on 1/22/15, 10:21 AM 752 views

In fact, I defined a function on "stock.picking", computes result for a functionnal field, on "stock.picking" object. this method browses account.move.line object, with ids of stock picking, is it correct ? 

Here after the code : 

class StockPicking(orm.Model):
    _inherit = 'stock.picking'
    _name = 'stock.picking'

    def _payed(self, cr, uid, ids, payedd, arg, context):

        rs= {}

        for picking in self.browse(cr, uid, ids, context):

            rs[picking.id] = False

            numbl = picking.num_bl
            move_line_obj = self.pool.get('account.move.line')

            for move_line in move_line_obj.browse(cr, uid, ids, context):

                if move_line.move_id.ref == numbl and move_line.reconcile_id:
                    rs[picking.id] = True
        return rs

    _columns = {
        'payedd' : fields.function(_payed, method=True, string='Encaisse', type='boolean'),
    }

 

 

I've got this error :

AttributeError: 'Field move_id not found in browse_record(account.move.line, 20058)'

It seems that when browsing withs ids of stock_picking, it does'nt recognize account_move_line model. If true, is there any workaround to get the field : reference of account_move_line object ?

0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 1/23/15, 4:36 AM

Your suspicion is correct that it is trying to browse account.move.line (through move_line_obj pool) with stock.picking's ids.

The question you need to ask first is which account.move.line are you going to pull?  The relationship need to be established first.  For example if you want to access related stock.move from stock.picking you can do this:

obj = self.pool.get('stock.picking').browse(cr, uid, ids[0], context=context)

stock_moves = obj.move_lines

Unfortunately specifically for account.move (and account.move.line) the official OpenERP v7 does not provide a definite relationship.  The only "clue" that you might be able to use is that the 'ref' field of account.move.line is filled with the related stock.picking's 'name' field (_create_account_move_line method in odoo/addons/stock/stock.py).  Note that AFAIK the account.move created is in 'draft' state and hence the 'ref' field can still be changed before posted and hence break the "relationship".

Assuming that you want to bet on the 'ref' field, here is the code:

    def _payed(self, cr, uid, ids, payedd, arg, context):

        rs= {}

        move_line_obj = self.pool.get('account.move.line')  # Put this outside of loop so it is not called repeatedly
        for picking in self.browse(cr, uid, ids, context):

            rs[picking.id] = False

            numbl = picking.num_bl

            move_line_ids = move_line_obj.search(cr, uid, [('ref', '=', picking.name)], context=context)
            for move_line in move_line_obj.browse(cr, uid, move_line_ids, context=context):

                if move_line.move_id.ref == numbl and move_line.reconcile_id:
                    rs[picking.id] = True
        return rs

Yes Thanks for answering, moves are posted automatically, on journal configuration , I choosed the option to skype the validation of moves, so I bet on the ref, I used a sql query to get the data from account_move_line, and it works, the way you did it is very intrestring for me, It's very usefull to know how to entertain the "relationship" using the orm, instead of sql queries, wich is a possible solution.

Yassine TEIMI
on 1/23/15, 5:07 AM

My pleasure.

Ivan
on 1/23/15, 5:37 AM
0

Ben Bernard

--Ben Bernard--
1083
| 4 3 6
Jakarta, Indonesia
--Ben Bernard--
Ben Bernard
On 1/23/15, 4:55 AM

You browse 'account.move.line' with ids which is the ids of 'stock.picking'?!

Yes, the key solution was to pull the ids of account_move_lines, and do a loop on them. (Ivan answer). Thanks.

Yassine TEIMI
on 1/23/15, 5:10 AM

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: 1/22/15, 10:21 AM
Seen: 752 times
Last updated: 3/16/15, 8:10 AM