Help

2

Make a field non-editable after Save

Avatar
aci aisha

Friends,

I have a field field_type.Its a many2one.I have to make it non-editable after save it.

Ex:When i create a file 1st time i set field_type as "Trademark"..

     Then when i edit it the field_type should be read only

Thanks...

Avatar
Discard
6 Answers
6
Avatar
Ivan
Best Answer

Redefine id as an integer field, put the id field in the view and use it in the attrs: attrs="{'readonly':[('id','>', 0)]}"

13 Comments
Avatar
Discard
Avatar
aci aisha
-

thanks ivan,but need som clarification

Avatar
Ivan
-

Sure @aci, what clarification do you need?

Avatar
aci aisha
-

what u mean by id?

Avatar
Ivan
-

When you define a model, Odoo/OpenERP will always add 5 additional columns (in some of the codes they call it Magic Columns: id, create_uid, create_date, write_date, write_uid) which will be controlled by ORM. id is the Database ID which will be the unique identifier of the record and Primary Key of the table representing the record and id is only assigned after the record is saved for the first time. It is implemented as serial in PostgreSQL and hence will start with 1 and continue until limit is reached, never recycled. The create_ pairs will record who and when the record is created (_uid will be a FK to res.users and _date is a timestamp field), while write_ pair will record who and when the record is updated last via ORM. Unfortunately you cannot just use the column 'id' without defining it in _columns. That's why I say you need to re-define the column first as an integer column.

Avatar
aci aisha
-

Ya Allah,,Confused!!!

Avatar
aci aisha
-

Ivan plz help

Avatar
Ivan
-

Have you tried the solution? Create an integer field, call it 'id', include it in the view, and use it in attrs?

Avatar
aci aisha
-

Ivan,try as u told,but the following error occured... Uncaught Error: Unknown field id in domain [["id",">",0]]

Avatar
Ivan
-

Have you include the 'id' field in the view?

Avatar
aci aisha
-

yes, 'id' : fields.integer('ID'),

Avatar
Ivan
-

That is not the view @aci. The view is defined in XML, part of which you put in your answer.

Avatar
aci aisha
-

Great Ivan..U R BRILLIANT!!!!

Avatar
Nebil Aydi
-

you are genius Mr Ivan

2
Avatar
Vasanth
Best Answer

Add a boolean field in the py fiel and add the field in the view and make it as invisible,then use the following code:

<field name="dummy_field" invisible="1" />

<field name="field_type"  attrs="{'readonly':[('dummy_field','=', True)]}" on_change="onchange_file_type(file_type)" />

In the onhange method,check if the field_type is true,then make the boolean (dummy_field) as true

else False

 

Avatar
Discard
1
Avatar
Anand
Best Answer

as empiro tech said.. add a state field like draft...

whenever you create the record it should be set to draft...

like _defaults={

        'state':'draft',

                          }

 

then in field set attrs="{'readonly':[('state','=','draft')]}"

3 Comments
Avatar
Discard
Avatar
Bole
-

This would be wery unprqactical... name state is reserved for states, and if you add more states , while using your code, the field would be readonly ONLY in draft state.... ( other models like account invoice , sales order etc...use reverse logic,,, 'readonly':[('state','!=',draft)]...

Avatar
Anand
-

functionally it will right.. but conceptually wrong may be...

Avatar
Anand
-

may be dummy field whic would do like this...

0
Best Answer

Try to add some kind of "state" and based on that you can make id readonly.

3 Comments
Avatar
Discard
Avatar
aci aisha
-

state is a field??

yes... ofcourse.. it is a field. Try looking the examples in sale order, purchase order, you will get better idea.

Avatar
aci aisha
-

But if i add a state field,Have i enter values there?

0
Avatar
aci aisha
Best Answer

Thank you Every1 for ur co-operation...

Friends,

When i use the following code it works,

                    <field name="file_type"  attrs="{'readonly':[('file_type','!=', False)]}" on_change="onchange_file_type(file_type)" /> 

That is when i select 1 value in file_type it becomes read only...

But when i try to save the file it cause the following error...!!

 

Odoo Warning

Integrity Error

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: file_type - file.type]

I also try this,,

    _columns = {
                'name' : fields.char('File No', readonly=True),
                #'file_type': fields.selection((('Trademark Registeration','Trademark Registeration'), ('Company Registeration','Company Registeration')),'File Type'),        
                'file_type': fields.many2one('file.type.details','File Type',required=True),
                'seq_id':fields.related('file_type','seq_id',type='char',string='Sequence ID'),        
                'application_no' : fields.char('Application No'),
                'classification_no' : fields.many2one('classification.details', 'Classification Number'),
                'classi_name' : fields.related('classification_no','classification_name',type='char',string= 'Classification Name'),
                'remarks' : fields.text('Remarks'),
                'file_status_id' : fields.many2one('file.status', 'Status', required=False),
                'a' : fields.char('a'),
                }
    _defaults={
                #'file_type' : _get_file_type
                'a' : 'a'

&

                    <field name="file_type"  attrs="{'readonly':[('a','=', 'a')]}" on_change="onchange_file_type(file_type)" /> 

But i cause the following error,

Error: Unknown field a in domain [["a","=","a"]]

 

1 Comment
Avatar
Discard
Avatar
Ivan
-

See my comment in @Bole's answer.

0
Avatar
Bole
Best Answer

The simplest way to do it is to define readonly state on the ffield ITSELF:.. in xml view definition... like:

<field name="your_field_name" attrs="{'readonly':[('your_field_name','=',False)]}" />

beware this will only allow to select value ONCE, and if you select wrong value, you need a mechanism to change it (just in case of error) . Such mechanism can be a wizard (allowerd for specific group  of users .. or something simmilar... )

Using field state to determine readonly state is most usual way to do it, but then the field remains in RW state until whole document changes state ... 

hop it helps..

2 Comments
Avatar
Discard
Avatar
aci aisha
-

how set wizard?

Avatar
Ivan
-

@Bole, I think that approach would not work. Once the field is populated, then it will be made readonly and hence the value would not be stored in the database. So, when the record is pulled up again, it would be empty and editable.