This question has been flagged
7 Replies
17891 Views

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
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)]}"

Avatar
Discard
Author

thanks ivan,but need som clarification

Sure @aci, what clarification do you need?

Author

what u mean by id?

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.

Author

Ya Allah,,Confused!!!

Author

Ivan plz help

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

Author

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

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

Author

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

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

Author

Great Ivan..U R BRILLIANT!!!!

you are genius Mr Ivan

Thank you!

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
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')]}"

Avatar
Discard

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)]...

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

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

Best Answer

You can do one thing:

First one in xml side:


Here id is compared with 0. If the record is created, it will have an integer id which will be greater than 0. And as per your requirement, when you are creating the record, the field will be editable. But when you save the record, id will be generated and the field will be readonly.1

Avatar
Discard
Author 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"]]

 

Avatar
Discard

See my comment in @Bole's answer.

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

Avatar
Discard
Author

how set wizard?

@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.

Best Answer

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

Avatar
Discard
Author

state is a field??

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

Author

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