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

Understanding the API: can I retrieve more fields while using search?

By
E.M.
on 2/6/16, 4:47 AM 586 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,

1
Yvan
On 2/6/16, 8:43 AM

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

E.M.
on 2/6/16, 9:05 AM
1
Jose M
On 2/6/16, 6:47 AM

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.

E.M.
on 2/6/16, 8:00 AM
0
Swapnesh
On 2/6/16, 5:45 AM

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

E.M.
on 2/6/16, 8:12 AM
0
E.M.
On 2/6/16, 8:05 AM

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

Jose M
on 2/6/16, 9:24 AM

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

E.M.
on 2/6/16, 10:31 AM

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

E.M.
on 2/6/16, 10:33 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: 2/6/16, 4:47 AM
Seen: 586 times
Last updated: 2/6/16, 9:06 AM