Odoo Help


How to get the value from that record order by date descending?

on 11/16/14, 11:17 AM 3,527 views

Hi all,

How to get the value from that record order by date descending to get the last value of my field "nextweek"?

in my table test_prod I have the following data http://i60.tinypic.com/w2nd3d.png

what I want is when I create new record I want to get the 70.00 value which is the last create_date of my prod_code and store to my begvalue field. 

The idea is to get that value which is the totalending of specific product and use as the beginning value of same product

could someone have a simple working code please post. 

something like this:

In create method, check the class if already any record exists order by id or not

2) if records exists, then browse the value from searched record and update it to your newly creating record.

3) if record does not exist, then you can give your own values to the initialbeginning and initialending  fields.


from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from osv import osv, fields
from openerp import tools
import time
class test_product(osv.Model):
    _name = "test.product"

      _columns = {

        'prodcode_id': fields.many2one("abs.prod.desc", 'Product Code'),
        'description_id': fields.many2one('abs.desc', 'Description'),
        'description': fields.related('description_id', type='many2one', relation='abs.desc', string='Description',),
        'price_id': fields.many2one('abs.price', 'Price', ondelete='cascade'),
        'price': fields.related('price_id','price_value', type='float', string='Price', ondelete='cascade'),
        'abs_weeklyperiod_id': fields.many2one('abs.weeklyperiod', 'Periods Covered', ondelete='cascade'),
        'newval': fields.float('New Val', digits=(12,2)),
    def onchange_prodcode(self, cr, uid, ids, prodcode_id, context=None):
        if prodcode_id:
            description_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).description_id.id
            price_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).price_id.id
            return {'value':{'description_id':description_id, 'price_id':price_id}}
        return {}

    def onchange_prodcode1(self,cr,uid,ids,prodcode,context=None):
        if prodcode:
            test_prod_ids = self.search(cr,uid,[('prodcode','=',prodcode)],order="id desc",limit=1,context=context)
            if test_prod_ids:
                rd_test_prod = self.read(cr,uid,test_prod_uids[0],['nextweek'],context=conetxt)
                return {'value':{'newval':rd_test_prod[0].get('nextweek')}}
            return {'value':{}}

class descriptionproduct(osv.osv):
    _description="Description Product"
    _name = 'abs.prod.desc'
    _columns = {
        'name': fields.char('Code', size=64, required=True,),
        'description_id': fields.many2one('abs.desc', 'Description', required=True),
        'price_id': fields.many2one('abs.price', 'Price', required=True),
class description(osv.osv):
    _name = 'abs.desc'
    _description = 'Description'
    _columns = {
        'name': fields.char('Description Name', size=64, required=True,),

class productprice(osv.osv):
    _description="Product Price"
    _name = 'abs.prod.price'
    _columns = {
        'price_id': fields.many2one('abs.price', 'Price', required=True),
        'price_value': fields.float('Price', digits=(12,2)),

class price(osv.osv):
    _name = 'abs.price'
    _description = 'Price'
    _columns = {
        'name': fields.float('Price', digits=(12,2)),

class abs_weeklyperiod(osv.Model):
    _name = "abs.weeklyperiod"

    _columns = {
        'start_date': fields.date('Start Date'),
        'end_date': fields.date('End Date'),
        'name': fields.char('Periods', size=32, required=True),
        'initialbeginning': fields.integer('Initial Beginning', size=32, ondelete='cascade'),
        'initialending': fields.integer('Initial Ending', size=32, ondelete='cascade'),

    _defaults = {
        'start_date': lambda *a: time.strftime('%Y-%m-%d'),
        'end_date': lambda *a: (datetime.today() + relativedelta(days=6)).strftime('%Y-%m-%d'),

Very Thanks



Hello philip

You can write an onchange_method for prodcode (which I assume is the many2one of product.product).

I am not sure in which model/screen you want to use this field but for now i am assuming it for the same table that you have specified. You can just modify the below code as per your requirement.

def onchange_prodcode(self,cr,uid,ids,prodcode,context=None)

    if prodcode:

        test_prod_ids = self.search(cr,uid,[('prodcode','=',prodcode)],order="id desc",limit=1,context=context)

        if test_prod_ids:

            rd_test_prod = self.read(cr,uid,test_prod_uids[0],['next_week'],context=conetxt)

            return {'value':{'yourfield':rd_test_prod[0].get('next_week')}}

    return {'value':{}}

I have used order by "id", you can use "create_date", if you want.

You can use same code by overriding the create method.

Hope this helps !!


Thanks I tried the code when I upgrade my module nothings happened, is there any I to my code, already update above code please

on 11/17/14, 3:46 AM

any update here

on 11/19/14, 12:26 PM

UPDATE PLEASE, NOT working with the current code Thanks

on 11/24/14, 10:50 AM

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.


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

1 follower(s)


Asked: 11/16/14, 11:17 AM
Seen: 3527 times
Last updated: 3/16/15, 8:10 AM