Odoo Help


Acessing a columns field from in a method

on 5/20/16, 6:14 AM 1,045 views

I used the attendance validation  in my custom class.I used @api.constrain that triggers following method when there is change to 's2' field

@api.constrains('s2')def constraint_action(self,cr, uid, ids, context=None):

for att in self.browse(cr, uid, ids, context=context):

# search and browse for first previous and first next records

prev_att_ids = self.search(cr, uid, [('EID', '=', att.EID), ('date', '<', att.date),

('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='date DESC')

next_add_ids = self.search(cr, uid, [('EID', '=', att.EID), ('date', '>', att.date),

('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='date ASC')

prev_atts = self.browse(cr, uid, prev_att_ids, context=context)

next_atts = self.browse(cr, uid, next_add_ids, context=context)

# check for alternance, return False if at least one condition is not satisfied

if prev_atts and prev_atts[0].action == att.action: # previous exists and is same action 

self._columns('state' '=' 'True')-----------------------------> This doesn't work

return True

I declared following variables at starting

class attendance(models.Model):

_name = "attendance.analysis"

_description = "attendance analysis"

_state = fields.boolean("state?",default = False)

_columns = { 'EID': fields.integer('Employee ID', required=True),

'action': fields.selection([('sign_in', 'Sign In'), ('sign_out', 'Sign Out')], 'Employee Action'),

'date': fields.datetime('Employee Date', required=True, select=1),


's2':fields.char("s2", required=True),



'state':fields.boolean("state?",default = False)


I want to access the state field in columns. How can I access it 

I want to change this boolean so that tree color can be changed on the view.xml.

Thanks in advance

In this link https://www.odoo.com/fr_FR/forum/aide-1/question/attributeerror-my-model-name-object-has-no-attribute-env-94569 they are accessing function written in old api from new api .But in my case I want to access the new api class column variable inside my old api function(def constraint_action(self,cr, uid, ids, context=None)

on 5/20/16, 8:15 AM
Atte Isopuro
On 7/8/16, 2:38 AM

Writing to a model's fields is done in two ways:

New-style API:

attendance_record.state = True

Old-style API:

attendance_record.write( { 'state': True } )

I'm not sure if writing will work inside a constrains-type method though. I also note your constrains-method never fails the validation. The documentation on the constrains-method \here\\&nbsp\;says\\


\Should\ raise\&nbsp\;\\ValidationError\\\

So\ any\ changes\ that\ fail\ your\ validation\ will\ still\ be\ made\ to\ the\ record\.\

If\ what\ you\ actually\ want\ is\ for\ the\ \'state\'\-field\ to\ change\ when\ some\ other\ fields\ change\,\ you\ might\ be\ looking\ for\&nbsp\;\onchange\\&nbsp\;or\&nbsp\;\computed fields (the second section of Basic Fields). These could be used to change the 'state' on the fly.

OK wow, odoo.com's formatter just did terrible things to my post. In case you can't make out what I said, here's the gist:

You write to fields using "record.field = 'value'" in the new API, or you use the 'write'-method, which works in bothe the old and new API's.

Your costrains-method should raise a ValidationError if you actually want to stop someone saving incorrect data: https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.api.constrains

You may however actually be looking for computed fields, as documented here: https://www.odoo.com/documentation/8.0/reference/orm.html#module-openerp.api

Atte Isopuro
on 7/8/16, 2:44 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: 5/20/16, 6:14 AM
Seen: 1045 times
Last updated: 7/8/16, 2:41 AM