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

Get single object

By
Sylwester Zalewski
on 10/28/15, 4:46 PM 443 views

Hi, sorry Iam new to odoo and python, so I have tree view with product history in the product equipment class which works fine but I want to also get first element value (product.history.customer) from the tree view but I have following error "ValueError: dictionary update sequence element #0 has length 1; 2 is required"

class product_history(osv.osv):

_name = 'product.history'

_description = 'product History'

_columns = {

'equipment_id': fields.many2one('product.equipment','Unit of work ref', required=True),

'customer': fields.many2one('res.partner', 'Customer',required=True),

'start_date': fields.date("Start Date"),

'end_date': fields.date("End Date"),

}

product_history()

class product_equipment(osv.osv):

_name = "product.equipment"

_description = "equipment"

_inherit = ['mail.thread','ir.needaction_mixin','product.history']

_columns = {

'main_location': fields.function(_last_name,type='many2one', string='Location', readonly = True),

}

def last_location(self, cr, uid, ids, field_name, arg, context=None):

return self.pool.get('product.history').browse(cr, uid, ids)

product_equipment()




0

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 10/29/15, 4:02 AM

from your question I do not see how to identify last location for particular product, but here is how to get last location

class product_equipment(osv.osv):
_name = "product.equipment"
_description = "equipment"
_inherit = ['mail.thread','ir.needaction_mixin','product.history']

_columns = {
'main_location': fields.function(_last_name,type='many2one',obj='product.history', string='Location',readonly=True),
}

def _last_name(self, cr, uid, ids, field_name, arg, context=None): res = {}
      for id in ids: res[id] =
self.pool.get('product.history').search(cr, uid, [ ], limit=1, order="create_date desc")
return res
product_equipment()

it'll find the latest location from all locations... but you'll need to add search domain inside [ ] of search function, in order to get last location for a particular product if you're going to track multiple products (for a single product it should work as is)...

please try above code first and check if it works in general with empty [ ] -search domain... then we'll need to implement correct domain, it should be something like:

[ ( 'id', 'in', location_ids) ]
where location_ids is variable containing all location ids related to current product. it may be retrieved from one2many field you use for display location list...
Temur
on 10/29/15, 4:18 AM

suggestion of search domain:

class product_equipment(osv.osv):
    _name = "product.equipment"
    _description = "equipment"
    _inherit = ['mail.thread','ir.needaction_mixin','product.history']

    _columns = {
        'main_location': fields.function(_last_name,type='many2one',obj='product.history', string='Location',readonly=True),
        'location_ids': fields.one2many('product.history', 'equipment_id', 'Location History'), 
# I think you have already "location_ids" field(maybe with different name), but it's not included in the question.... if so, please adapt field name to your case, otherwise add this field...
    }

    def _last_name(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for id in ids:
            location_ids = self.browse(cr, uid, id, context).location_ids.ids
            res[id] = self.pool.get('product.history').search(cr, uid, [( 'id', 'in', location_ids) ], limit=1, order="create_date desc")
        return res
product_equipment()
Temur
on 10/29/15, 4:32 AM

hi, Thanks for your help!! but I still have some problems, because I cant see the result but if I change the type in "main_location" from many2one to e.g char (which shows the ID) I can see the correct ID so I dont know how to fix that. Also I would like to know if I can show the customer not the name from class history.

Sylwester Zalewski
on 10/31/15, 12:06 PM

either:

'main_location': fields.function(_last_name,type='many2one',obj='product.history', string='Location',readonly=True),
OR
'main_location': fields.function(_last_name,type='many2one', relation='product.history', string='Location',readonly=True),
should work.. make sure you add obj='product.history' (or relation=) part to the field definition.
Temur
on 11/2/15, 1:31 AM

That's what I did but it doesn't work, I don't know why, it looks like empty space... any suggestions ?

Sylwester Zalewski
on 11/2/15, 2:01 PM

to 'product.history' object definition add the following line:

_rec_name = "customer"
-it'll show customer instead of name of product.history record... and try with obj='product.history' in 'main_location' field definition, without relation='product.history', do not forget to restart odoo and then update/upgrade module from "Settings/Modules/Local Modules" page as you make changes.
Temur
on 11/3/15, 1:58 AM
0

Axel Mendoza

--Axel Mendoza--
10205
| 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 10/28/15, 6:18 PM

It's not clear what you are trying to do, but here are some corrections in bold:

class product_equipment(osv.osv):
_name = "product.equipment"
    _description = "equipment"
    _inherit = ['mail.thread','ir.needaction_mixin','product.history']

    _columns = {
        'main_location': fields.function(_last_name,type='many2one',relation='product.history', string='Location',readonly=True),
    }

    def _last_name(self, cr, uid, ids, field_name, arg, context=None):
        return self.pool.get('product.history').browse(cr, uid, ids[0])
product_equipment()

unfortunately its not working and same error appears. I will try to explain it better so - I have history list at the bottom in the equipment class and I want to show the last added element outside the list (in the form to show current customer )

Sylwester Zalewski
on 10/28/15, 6:53 PM

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

2 follower(s)

Stats

Asked: 10/28/15, 4:46 PM
Seen: 443 times
Last updated: 10/29/15, 4:03 AM