Help

1

How to handle reading and writing to read-only fields

Avatar
Thibaut DIRLIK

Hi,

I'm creating a reference field, containing an auto-generated sequence generated by a _defaults lambda which gets the sequence value.

The problem is that when the view is loaded, the sequence is set in the field. But because the field is readonly, when the object is saved, the sequence is re-generated by create() because the field value was not in vals{}.

Is it possible to say "do not load the default value in the view" ? This way, only the call to create() would grab the sequence value.

2 Comments
Avatar
Discard
Avatar
Daniel Reis
-

What's the point of setting a _default if you don't want it?

Avatar
Thibaut DIRLIK
-

I want it. I want the reference to be generated automatically. The problem is that OpenERP generates it twice if I set my field readonly : Once when loading the view, and once when calling create().

2 Answers
10
Avatar
Olivier Dony (odo)
Best Answer

Read-only fields are meant for informative purpose only, so they're not saved by the OpenERP client-side when persisting records, this is a known limitation [1].

It's a design decision, and it has not been decided to change it so far, so developers must not rely on them for persisting field values.

The usual cases where developers request this feature are the following:

  1. For default values that are read-only and have side-effects when computed twice, typically sequence numbers
  2. For on_change results that are displayed read-only to the user and not otherwise computed via function fields

Both cases may seem good on first sight but they're based on a logic that is a bit flawed:

  1. If the code that computes the _default has side-effects, those effects will be produced eventually, e.g. when someone discards a new record without saving it. The usual solution is to only compute the function with the side-effects when the record is finally saved, or when the record goes through a certain workflow transition (similarly to account.invoice).
  2. on_change calls are meant to assist the user during the data input, but should not be used as a replacement for computed fields on the business logic side. Look at sale.order or account.invoice for example, the line subtotal is updated only after saving, making sure everything is properly taken into account. An update of the value could be done via an on_change, but that would only provide a visual hint for the user, not become the reference value.

There are many workarounds for accomplishing this (e.g. using a second invisible field to transmit the actual value), but it is probably better to avoid those cases altogether. As a rule of thumb, try to only use the read-only flag for:

  • functional fields (it is the default anyway)
  • fields that are changed by the business logic/workflow but not the UI (typically, the state field)
  • fields that are disabled/ignored

It's still fine to update them via on_change calls, but don't rely on this mechanism for storing the value.

Also note that the proposal for a new OpenERP API includes merging the _defaults, the on_change and the fields.function into a generic "derived value" concept with a common implementation [2], which should make this whole mess a lot more straightforward.

[1] see bug 378824 and probably some other related ones
[2] see this slide and this presentation about the new API

5 Comments
Avatar
Discard
Avatar
Thibaut DIRLIK
-

Thanks for your reference answer. I'm sure it will help other people.

Avatar
Thibaut DIRLIK
-

I just read the [2] link and it seems very promising. Do you have any idea of the release it will be introduced in ? Would solve a LOT of problems that we have today.

Avatar
Olivier Dony (odo)
-

The new API implementation is an ongoing work-in-progress, with no ETA yet. You can follow the progress on the development branches, e.g. here

Avatar
priyankahdp
-

thanks Dony. please advice me to how set field attr to readonly by function.?

Avatar
Vasiliy Birukov
-

Dear Oliver, can you advise right way for use function field as readonly and update it via on_change inside one2many lines? Because when we close popup form of line with saving and then reopen without saving of main form, then we lose changes from on_change.

0
Best Answer

Hello

I hope below code will help you

In my case have Total year field is readonly and based on 'Date of birth' Total year will be update

Using onchange method, Can get Total year on field but when save that record Total Year field to set blank

Solution:-

Create new dummy field of total year and set that dummy field value on original field

Example:-

Python file

total_year = fields.Float()

total_year_copy = fields.Float()

from datetime import date​​

# Onchange method

@api.onchange('dob')
def onchange_dob(self):
today = date.today()
self.total_year = self.total_year_copy = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))

# Create method

@api.model

def create(self, vals):

if 'total_year_copy' in vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).create(vals)

# Write method

@api.multi

def write(self, vals):

if 'total_year_copy' in vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).write(vals)

Xml File

<field name="total_year" readonly="1"/>

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

Hope this help you to save readonly records

Best Regards,

Ankit H Gandhi

Avatar
Discard