Odoo Help

0

Get single object

By
Sylwester Zalewski
on 10/28/15, 4:46 PM 677 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
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
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

About This Community

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

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

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