Inspection and Introspection¶
While we previously used fields_get() to query a model and have been using an arbitrary model from the start, Odoo stores most model metadata inside a few meta-models which allow both querying the system and altering models and fields (with some limitations) on the fly over REST API.
Note
ir.model¶
Provides information about Odoo models via its various fields
- name
- a human-readable description of the model
- model
- the name of each model in the system
- state
- whether the model was generated in Python code (
base
) or by creating anir.model
record (manual
) - field_id
- list of the model’s fields through a One2many to ir.model.fields
- view_ids
- One2many to the Views defined for the model
- access_ids
- One2many relation to the Access Control set on the model
Note
ir.model
can be used to:
- query the system for installed models (as a precondition to operations on the model or to explore the system’s content)
- get information about a specific model (generally by listing the fields associated with it)
- create new models dynamically over REST API
Warning
- custom model names must start with
x_
- the
state
must be provided andmanual
, otherwise the model will not be loaded - it is not possible to add new methods to a custom model, only fields
Example¶
Create
x_custom_model
model record inir.model
object using Create Records API endpoint.Request:
POST /restapi/1.0/object/ir.model?vals={'name':'Custom Model','model':'x_custom_model','state':'manual'} HTTP/1.1 Host: {your_Odoo_server_url}
Response:
HTTP/1.1 200 OK { 'Models': { 'id': 104, 'name': 'Custom Model', 'model': 'x_custom_model', 'state': 'manual' ... ... ... } }
Inspect a model
x_custom_model
’s fields using Listing Record Fields API endpoint.
Request:
GET /restapi/1.0/object/x_custom_model/fields_get?attributes=['string','help','type'] HTTP/1.1 Host: {your_Odoo_server_url}Response:
Note
a custom model will initially contain only the “built-in” fields available on all models
HTTP/1.1 200 OK { 'fields': { 'create_uid': { 'type': 'many2one', 'string': 'Created by' }, 'create_date': { 'type": 'datetime', 'string': 'Created on' }, '__last_update': { 'type': 'datetime', 'string': 'Last Modified on' }, 'write_uid': { 'type': 'many2one', 'string': 'Last Updated by' }, 'write_date': { 'type': 'datetime', 'string': 'Last Updated on' }, 'display_name': { 'type': 'char', 'string': 'Display Name' }, 'id": { 'type': 'integer', 'string': 'Id' } } }
ir.model.fields¶
Provides information about the fields of Odoo models and allows adding custom fields without using Python code
- model_id
- Many2one to ir.model to which the field belongs
- name
- the field’s technical name (used in
read
orwrite
) - field_description
- the field’s user-readable label (e.g.
string
infields_get
) - ttype
- the type of field to create
- state
- whether the field was created via Python code (
base
) or viair.model.fields
(manual
) - required, readonly, translate
- enables the corresponding flag on the field
- groups
- field-level access control, a Many2many to
res.groups
- selection, size, on_delete, relation, relation_field, domain
- type-specific properties and customizations, see the fields documentation for details
Note
Like custom models, only new fields created with state="manual"
are activated as actual fields on the model.
Warning
computed fields can not be added via ir.model.fields
, some field meta-information (defaults, onchange) can not be set either
Example¶
Create
x_custom
model record inir.model
object using Create Records API endpoint.Request:
POST /restapi/1.0/object/ir.model?vals={'name':'Custom Model','model':'x_custom','state':'manual'} HTTP/1.1 Host: {your_Odoo_server_url}
Response:
HTTP/1.1 200 OK { 'Models': { 'id': 105, 'name': 'Custom Model', 'model': 'x_custom', 'state': 'manual' ... ... ... } }
Create
x_name
field record inir.model.fields
object using Create Records API endpoint.Request:
POST /restapi/1.0/object/ir.model.fields?vals={'model_id':105,'name':'x_name','ttype':'char','state':'manual','required':True} HTTP/1.1 Host: {your_Odoo_server_url}
Response:
HTTP/1.1 200 OK { 'Fields': { 'id': 210, 'name': 'x_name', 'model_id': [105, 'Custom Model'], 'ttype': 'char', 'state': 'manual', 'required': True ... ... ... } }
Create
test record
record inx_custom
object using Create Records API endpoint.Request:
POST /restapi/1.0/object/x_custom?vals={'x_name':'test record'} HTTP/1.1 Host: {your_Odoo_server_url}
Response:
HTTP/1.1 200 OK { 'Custom Model': { 'id': 115, 'x_name': 'test record', 'display_name': 'test record', 'create_date': '2017-07-15 14:31:17', 'create_uid': [1, 'Administrator'], 'write_date': '2017-07-15 14:31:17', 'write_uid': [1, 'Administrator'], ... ... ... } }