Skip to Content
Menu
This question has been flagged
2 Replies
5692 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),

's1':fields.char("s1"),

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

's3':fields.char("s3"),

's4':fields.char("s4"),

'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

Avatar
Discard
Author

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)

Best Answer

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.

Avatar
Discard

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

Related Posts Replies Views Activity
1
Mar 15
8770
1
Dec 24
54
1
Nov 24
175
3
Sep 24
254
4
Apr 24
170712