콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
6 답글
7557 화면

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
3765
2
2월 16
3413
2
2월 22
5500
2
9월 15
4578
0
5월 15
3088