İçereği Atla
Menü
Bu soru işaretlendi
6 Cevaplar
7341 Görünümler

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
Vazgeç
En İyi Yanıt

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
Vazgeç
Üretici

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

En İyi Yanıt

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
Vazgeç
Üretici

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.

En İyi Yanıt

Hello EM,

You can access other stock_move fields same like id

Example:

field name: name

for move in moves:
     print move.name

Avatar
Vazgeç
Üretici

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

Üretici En İyi Yanıt

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
Vazgeç

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

Üretici

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

Üretici

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

İlgili Gönderiler Cevaplar Görünümler Aktivite
1
Tem 18
3673
2
Şub 16
3279
2
Şub 22
5358
2
Eyl 15
4353
0
May 15
2964