Odoo Help


How to create method to check the class if already any record exists order by id and update it to newly created record?

on 11/28/14, 1:08 PM 1,739 views

Hi All,

Please help cannot get to work on this:

My senarios:

1) In create method, check the class if already any record exists order by id
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 fields.

or something like:

In create method, check the test_weeklyperiod class
    if "ending field is >0 order by id" or write_date
        then get ending value and update it to beginning field.
    elif "initialending is >0 order by id" or write_date
    then get initialending value and update it to beginning field.
    else "initialending is equal to 0
    then give the value to initialending field.

in summary want to achieve is, the value of ending field in the previous summary records with same prodcode is also the beginning of the current new created record of same prodcode, hope this clear.

anyone who can suggest simple senarios like this I am very thankful

Thanks for reading

Here is My Python:

class test_product(osv.Model):
    _name = "test.product"

    def get_ending(self, cr, uid, ids, name, arg, context):
        res = {}
        for endings in self.browse(cr, uid, ids, context):
            res[endings.id] = endings.beginning *2
            return res

    _columns = {
        'prodcode_id': fields.many2one("test.prodcode", 'Product Code'),
        'weeklyperiod_id': fields.many2one('test.weeklyperiod', 'Periods Covered', ondelete='cascade'),
        'price': fields.float('Price', digits=(12,2)),
        'beginning': fields.integer('Beginning'),
        'ending': fields.function(get_ending, method=True,type='integer',string='Ending', store=True),

def onchange_prodcode(self,cr,uid,ids,prodcode_id,context=None):
        if prodcode_id:
            test_prod_ids = self.search(cr,uid,[('prodcode_id','=',prodcode_id)],order="id desc",limit=1,context=context)
            if test_prod_ids:
                rd_test_prod = self.read(cr,uid,test_prod_ids[0],['weeklyperiod_id.initialbeginning'],context=context)
                return {'value':{'beginning':rd_test_prod[0].get('weeklyperiod_id.initialbeginning')}}
        return {'value':{}}

class weeklyperiod(osv.Model):
    _name = "test.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'),

class test_prodcode(osv.Model):
    _name = "test.prodcode"
    _columns = {
        'name': fields.char('Periods', size=32, required=True),

also my XML:

    <record id="test_product_form_view" model="ir.ui.view">
        <field name="name">test.product.form.vew</field>
        <field name="model">test.product</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Product" version="7.0">
                    <field name="prodcode_id" class="oe_no_button" placeholder="Product Code" style="width: 37%%" on_change="onchange_prodcode(prodcode_id)"/>
                    <field name="price" class="oe_no_button" placeholder="Price" style="width: 37%%"/>
                    <field name="weeklyperiod_id" class="oe_no_button" placeholder="Weekly Period" style="width: 37%%"/>
                    <field name="beginning" class="oe_no_button" placeholder="Beginning" style="width: 37%%"/>
                    <field name="ending" class="oe_no_button" placeholder="Ending" style="width: 37%%"/>

    <record id="test_product_tree_view" model="ir.ui.view">
        <field name="name">test.product.tree.view</field>
        <field name="view_type">tree</field>
        <field name="model">test.product</field>
        <field name="arch" type="xml">
        <tree string="Inventory">
              <field name="prodcode_id" string="CODE"/>
              <field name="price" string="Price"/>
              <field name="weeklyperiod_id" string="Weekly Period"/>
              <field name="beginning" string="Beginning"/>
              <field name="ending" string="Ending"/>


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

2 follower(s)


Asked: 11/28/14, 1:08 PM
Seen: 1739 times
Last updated: 3/16/15, 8:10 AM