How to handle reading and writing to read-only fields
I'm creating a
referencefield, containing an auto-generated sequence generated by a
_defaultslambda 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
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.
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 .
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:
- For default values that are read-only and have side-effects when computed twice, typically sequence numbers
- 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:
- 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).
- 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 , which should make this whole mess a lot more straightforward.
- What documentation is available for odoo?
- The different "openerp model inheritance" mechanisms: what's the difference between them, and when should they be used ?
- What is the different between col and colspan?
- How can I activate the developer mode?
- How i can create module openerp 7 / Odoo 8 ?
- How css and js inheritance works on Odoo v8?
- How to display database name without debug mode?
- How to run tests
- Relative Date (today, this month, e.g.) in Filter
- How to disable developer mode or debugging feature in openerp 7
- Hide menu for existing group
- How to show Thumbnail image in list view?
- Please define these Python Fetch types
- What is the _track proprerty of mail.thread model used for ?
- why dummy field used in product.product
- How to add a custom css?
- How to move around workflow states back and forth?
- Setting up rules' Access Right in xml,csv files
- Anyone has realized a locking functionality?
- How to override a global function?
- Show Account Number(Partial or Semi Visible Number) Formatting in OpenERP 7.0
- how to override name_get method in new api
- How to extend fields.selection options without overwriting them?
- How to enable a security group for all users?
- How to detect which modules are installed
- Details and examples on the accounting API?
- How to debug Odoo in Eclipse under Winows?
- Whats the difference between inherit and inherits?
- What should onchange methods do and return?