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.

1

[SOLVED] New API Search/Browse - how?

By
Dr Obx
on 3/4/16, 7:54 AM 1,327 views

Guys, I'm a beginner so sometimes I have a problem with some things. I would like to get a value from sale_order_line.

In my table I have a column named: item_location

How to get data from this column?

class sale_order_line(models.Model):
    _inherit = 'sale.order.line'

    item_location = fields.Char(compute='get_location', string = 'Item location', type='Char')

    def get_location(self):
        location = self.env['sale.order.line'].search([('product_id','=',self.product_id.id)])
        print ' Self product ID :', self.product_id.id# Ok
        print ' Self ID :', self.id # ok
        print ' Location ID :', location.id # ok
        print ' Location name :', location.item_location # False
        res = location.item_location # this is what I need

        return res

sale_order_line()

but I still missing something because I can't get a value from the item_location column.

How to do it with a new API ?

GOT IT!

You guys are right, I don't have to do it using Search or Browse in this case ;) because that field is available directly :)

Ii's another valuable lesson for me, THANK YOU GUYS


I managed to make it works but for some reason I can't see it in order form(table), any ideas why ?

Dr Obx
on 3/4/16, 9:14 AM

(I edited your code you readability.)

PY
on 3/4/16, 9:16 AM

PY: Where ?

Dr Obx
on 3/4/16, 9:21 AM

just the display, your code, can't you see the code is now in a proper code block (pre tag) ? easier to read when well indented and all

PY
on 3/4/16, 9:27 AM

Yes I think everything is done correctly, I still cant see the content of 'location' field. But I can see "item_location" instead so 'location' is not required anymore ;) Thanks for help :)

Dr Obx
on 3/4/16, 9:30 AM
3

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 3/4/16, 9:01 AM

DR Obx,


If you are calling your method on compute attribute, it will work like functional field.

This method will automatically load for sale order line model self, because you already using that method in same model so you don't need to call search method in sale.order line.


Here you go,

Do one thing,

set api.multi decorator on your method.

So, your method will look lik,

@api.multi
get_location(self):
for line in self:
#self will work for all sale order line you can directly check in line record, line itself will appear as browse record

Second note, if your sale order line does not having any location value in that case of course it will give you false value, you have to put if condition for that, if you location than move further for next execution.

Hope this might help you.


Regards,

Anil.

Thank you Anil, So I don't need search just pick self.item_location ? :)

Dr Obx
on 3/4/16, 9:05 AM
2

Axel Mendoza

--Axel Mendoza--
10203
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 3/4/16, 8:27 AM

Hi @Dr Obx

You need put the model of that query and the domain like:

location = self.env['sale.order.line'].search([('product_id','=',self.product_id.id)])

bolded are the changes to your code, the model is sale.order.line like every other api model method interaction and the domain need to be changed to pass exactly the id value of the product_id relation field because the domain parser will not do it for you, you need to pass exactly the value in the param #3 of the domain tuple.

Also if your item_location field is on the sale.order.line model you need to access it like:

location.item_location

if it's on the product.product model then you could get it (using your actual code) like:

location.product_id.item_location

but you could directly access the product if you don't need the sale.order.line changing your search like:

product = self.env['product.product'].search([('id','=',self.product_id.id)])
item_location = product.item_location

this last is guessing where is located the field item_location because was not clear to me in your answer

Hope this helps

********************************** Update*****************************************

Seeing your code changes seems that you are doing more than needed for get the item_location.

1- The item_location is on your sale.order.line so you could access it directly from the outside of the model, you don't need a computed field for that.

2- The search is not necessary because search usually is for get the ids and you already have the id, so a search with that domain will produce the same self.id value but with the browse applied so you could directly use the browse like:

location = self.env['sale.order.line'].browse(self.id)

But I repeat you could directly get the value from the outside without using a computed field. Also for the same thing there is a field definition way that will cause that Odoo get the value of others fields in the table or walking through many2one relations fields, like:

item_location = fields.Char(related='item_location', string = 'Item location')

And nothing more is needed

The thing appears to be that the sale order line doesn't have a value for the item_location field

Hi Axel, I changed it a bit but it seems not working. I can browse the name, state etc but I can't get item_location. Field is in table 100% but for some reason i cant get the value from it ;(

from openerp import fields, tools, models, api, _

class sale_order_line(models.Model):
	_inherit = 'sale.order.line'
	item_location = fields.Char()
	location = fields.Char(compute='get_location', string = 'Location', type='Char')
	
	def get_location(self):
		location = self.env['sale.order.line'].search([('id','=',self.id)])
		print '== item location ==============================================================='
		print '   Self product ID   :', self.product_id.id
		print '   Product name      :', location.name
		print '   Location ID       :', location.id
		print '   Location name     :', location.item_location
		print '================================================================================'
		res = location.id
		return res
sale_order_line()
Result:
== item location ===============================================================
   Self product ID   : 694
   Product name      : Intel Celeron D 331 2.66GHz 256K FSB 533Mhz Skt LGA 775 pin SL7TV CPU Processor
   Location ID       : 425
   Location name     : False
================================================================================
Dr Obx
on 3/4/16, 8:39 AM

please see my answer update

Axel Mendoza
on 3/4/16, 8:58 AM

I can see it printed on screen but I can't see it in Odoo ...

Dr Obx
on 3/4/16, 9:07 AM

worked then?

Axel Mendoza
on 3/4/16, 9:16 AM

Yes Axel it worked but there was no data in table 'item_location' :) That's why I was getting 'False' instead of the actual location name :) But like I said, I can see it now on screen in CMD but it won't show location name in form ;(

Dr Obx
on 3/4/16, 9:21 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: 3/4/16, 7:54 AM
Seen: 1327 times
Last updated: 11/8/16, 5:32 AM