Skip to Content
Menu
This question has been flagged
6 Replies
6266 Views

If I do:

moves = env['stock.move'].search([('picking_id','=',current_stock_picking)])

I retrieve the list of id's of moves with a given picking_id.

And I can do things like:

for move in moves:
  print move.id

So far, so good. But, is it possible to retrieve more fields than id while searching? Or do I have to later retrieve the fields for every id? If so, how?

I obviously want to use other stock_move fields -such as, for example, origin-, using just:

move.origin

Thanks,

Avatar
Discard
Best Answer

Hi E.M.

With the search method you will only get a list of ids. If you want to search and retrieve data in a single step, you should use the search_read method.

moves = env['stock.move'].search_read([('picking_id','=',current_stock_picking)])
for move in moves:
print move.id
print move.name

If you want to print the whole result in a human readable way, the pretty print module is you friend.

from pprint import pprint

moves = env['stock.move'].search_read([('picking_id','=',current_stock_picking)])
pprint(moves)

Best regards

Yvan

Avatar
Discard
Author

Thanks, this really helps. search_read does the work. However I have noticed that if I do for move in moves: print move.origin does not work (-print move['origin'] works-). For some reason I thought it would be possible to use move.origin

Best Answer

Hi EM, 

If you cant access more fields, it's because you are not using properly the new api and your search is returning only the id's.  Where did you get your env used for env['stock.move']?

Anyway, you can still use all the fields by using the id's in a browse, like:

for move in env['stock.move'].browse(moves):
     print move.whatever

Avatar
Discard
Author

I am getting env through env = Env(cr, uid, context). I am sure I am mixing stuff, find attached my code in my edited answer in case you want to highlight how should it be done.

Best Answer

Hello EM,

You can access other stock_move fields same like id

Example:

field name: name

for move in moves:
     print move.name

Avatar
Discard
Author

Unfortunatelly it does not work, I guess I am mixing something as pointed by Jose M. Find my whole controller code in my answer. Thanks

Author Best Answer

This is the whole code I am using, I guess I am mixing old and new API. I would like to use new API.

It is a controller. It is activated by clicking a button in stock_picking, retrieves current stock_picking and I am looking to retrieve all stock moves associated to that stock_picking.


class Binary(http.Controller):
@http.route('/web/binary/item_labels', type='http', auth="user")
@serialize_exception
    def download_item_labels(self, id, **kw):
        
        cr, uid, context = request.cr, request.uid, request.context
env = Env(cr, uid, context)
        
        Model = request.registry['stock.picking']
        fields = [ 'id' ]
        current_stock_picking = Model.read(cr, uid, [int(id)], fields, context)[0]['id']
        print "download_item_labels"
        print "current_stock_picking"
        print current_stock_picking

        moves = env['stock.move'].search([('picking_id','=',current_stock_picking)])
        print "moves"
        print moves
        for move in moves:
            print "move"
            print move
            print move.id
            #print move.source <---- THIS FAILS!


Any help / comments are truly appreciated,


Avatar
Discard

Do what i tould you before, use a browse: for move in env['stock.move'].browse(moves) print move.source

Author

That does not work in this case, maybe there is something being mix between APIs, that's why I posted the whole controller code.

Author

ProgrammingError: can't adapt type 'stock.move'\n", "message": "can't adapt type 'stock.move'", "name": "psycopg2.ProgrammingError", "arguments": ["can't adapt type 'stock.move'"]}} I wonder if this being inside a controller makes a difference or not..

Related Posts Replies Views Activity
1
Jul 18
2959
2
Feb 16
2566
2
Feb 22
4289
2
Sep 15
3289
0
May 15
2244