跳至内容
菜单
此问题已终结
6 回复
7418 查看

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,

形象
丢弃
最佳答案

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

形象
丢弃
编写者

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

最佳答案

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

形象
丢弃
编写者

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.

最佳答案

Hello EM,

You can access other stock_move fields same like id

Example:

field name: name

for move in moves:
     print move.name

形象
丢弃
编写者

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

编写者 最佳答案

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,


形象
丢弃

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

编写者

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

编写者

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..

相关帖文 回复 查看 活动
1
7月 18
3719
2
2月 16
3329
2
2月 22
5409
2
9月 15
4428
0
5月 15
3027