Odoo Help

Welcome!

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

0

Acessing a columns field from in a method

By
inian
on 5/20/16, 6:14 AM 395 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

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)

inian
on 5/20/16, 8:15 AM
1
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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/20/16, 6:14 AM
Seen: 395 times
Last updated: 7/8/16, 2:41 AM