This question has been flagged
3 Replies
9694 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()




Avatar
Discard
Best Answer

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

Avatar
Discard

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

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()
Author

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.

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

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

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

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()
Avatar
Discard
Author

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 )