This is the documentation for older versions of Odoo (formerly OpenERP).

See the new Odoo user documentation.

See the new Odoo technical documentation.


ORM methods



https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/methods/





Keeping the context in ORM methods

In OpenObject, the context holds very important data such as the language in which a document must be written, whether function field needs updating or not, etc.

When calling an ORM method, you will probably already have a context - for example the framework will provide you with one as a parameter of almost every method. If you do have a context, it is very important that you always pass it through to every single method you call.

This rule also applies to writing ORM methods. You should expect to receive a context as parameter, and always pass it through to every other method you call..



ORM methods


class  osv.osv. osv ( pool,  cr )


osv.osv.osv_base, osv.orm.orm


browse ( cr,  uid,  select,  context=None,  list_class=None,  fields_process=None )


Fetch records as objects allowing to use dot notation to browse fields and relations

Parameters:

  • cr
  • user
  • select
  • context

Return type:

object or list of objects requested

check_access_rule ( cr,  uid,  ids,  operation,  context=None )


operation

Parameters:

operation -- one of write, unlink

Raises except_orm:

 


  • if current ir.rules do not permit this operation.


Returns:

None if the operation is allowed

copy ( cr,  uid,  id,  default=None,  context=None )


Duplicate record with given id updating it with default values

Parameters:

  • cr
  • uid
  • id
  • default (dictionary) -- dictionary of field values to override in the original values of the copied record, e.g: {'field_name': overriden_value, ...}
  • context (dictionary) -- context arguments, like lang, time zone

Returns:

True

copy_data ( cr,  uid,  id,  default=None,  context=None )


Copy given record's data with all its fields values

Parameters:

  • cr
  • user
  • id
  • default (dictionary) -- field values to override in the original values of the copied record
  • context (dictionary) -- context arguments, like lang, time zone

Returns:

dictionary containing all the field values

create ( cr,  user,  vals,  context=None )


Create new record with specified value

Parameters:

  • cr
  • user (integer) -- current user id
  • vals (dictionary) -- field values for new record, e.g {'field_name': field_value, ...}
  • context (dictionary) -- optional context arguments, e.g. {'lang': 'en_us', 'tz': 'UTC', ...}

Returns:

id of new record created

Raises:

  • AccessError
  • if user has no create rights on the requested object
  • if user tries to bypass access rules for create on the requested object
  • ValidateError
  • UserError

Note: The type of field values to pass in vals for relationship fields is specific. Please see the description of the write() method for details about the possible values and how to specify them.

default_get ( cr,  uid,  fields_list,  context=None )


Returns default values for the fields in fields_list.

Parameters:

  • fields_list (list) -- list of fields to get the default values for (example ['field1', 'field2',])
  • context -- optional context dictionary - it may contains keys for specifying certain options like context_lang (language) or context_tz (timezone) to alter the results of the call. It may contain keys in the form default_XXX (where XXX is a field name), to set or override a default value for a field. A special bin_size boolean flag may also be passed in the context to request the value of all fields.binary columns to be returned as the size of the binary instead of its contents. This can also be selectively overriden by passing a field-specific flag in the formbin_size_XXX: True/False where XXX is the name of the field. Note: Thebin_size_XXX

Returns:

dictionary of the default values (set on the object model class, through user preferences, or in the context)

export_data ( cr,  uid,  ids,  fields_to_export,  context=None )


Export fields for selected objects

Parameters:

  • cr
  • uid
  • ids
  • fields_to_export
  • context

Return type:

datas

This method is used when exporting data via client menu

fields_get ( cr,  user,  fields=None,  context=None )


Get the description of list of fields

Parameters:

  • cr
  • user
  • fields
  • context

Returns:

dictionary of field dictionaries, each one describing a field of the business object

Raises AccessError:

 

  • if user has no create/write rights on the requested object


fields_view_get ( cr,  user,  view_id=None,  view_type='form',  context=None,  toolbar=False,  submenu=False )


Get the detailed composition of the requested view like fields, model, view architecture

Parameters:

  • cr
  • user
  • view_id
  • view_type
  • context
  • toolbar
  • submenu

Returns:

dictionary describing the composition of the requested view (including inherited views and extensions)

Raises:

  • AttributeError
  • if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
  • if some tag other than 'position' is found in parent view
  • Invalid ArchitectureError

get_xml_id ( cr,  uid,  ids,  *args,  **kwargs )


osv.osv.get_xml_id.

When multiple XML IDs exist for a record, only one of them is returned (randomly).

Synopsis: get_xml_id(cr, uid, ids) -> { 'id': 'module.xml_id' }

Returns:

map of ids to their fully qualified XML ID, defaulting to an empty string when there's none (to be usable as a function field).

import_data ( cr,  uid,  fields,  datas,  mode='init',  current_module='',  noupdate=False,  context=None, filename=None )


Import given data in given module

Parameters:

  • cr
  • uid
  • fields
  • data
  • mode
  • current_module
  • noupdate
  • context
  • filename

Return type:

tuple

This method is used when importing data via client menu.

Example of fields to import for a sale.order:



.id, (=database_id) partner_id, (=name_search) order_line/.id, (=database_id) order_line/name, order_line/product_id/id, (=xml id) order_line/price_unit, order_line/product_uom_qty, order_line/product_uom/id (=xml_id)

name_get ( cr,  user,  ids,  context=None )


Parameters:

  • cr
  • user (integer) -- current user id
  • ids
  • context (dictionary) -- context arguments, like lang, time zone

Returns:

tuples with the text representation of requested objects for to-many relationships

name_search ( cr,  user,  name='',  args=None,  operator='ilike',  context=None,  limit=100 )


Search for records and their display names according to a search domain.

Parameters:

  • cr
  • user
  • name
  • args
  • operator
  • context (dictionary) -- context arguments, like lang, time zone
  • limit

Returns:

list of object names matching the search criteria, used to provide completion for to-many relationships

This method is equivalent of search() on name + name_get() on the result. See search() for an explanation of the possible values for the search domain specified in args.

perm_read ( cr,  user,  ids,  context=None,  details=True )


Returns some metadata about the given records.

Parameters:

details

Returns:

list of ownership dictionaries for each requested record

Return type:

list of dictionaries with the following keys:

  • id: object id
  • create_uid: user who created the record
  • create_date: date when the record was created
  • write_uid: last user who changed the record
  • write_date: date of the last change to the record
  • module.name


read_group ( cr,  uid,  domain,  fields,  groupby,  offset=0,  limit=None,  context=None,  orderby=False )


groupby

Parameters:

  • cr
  • uid
  • domain
  • fields
  • groupby
  • offset
  • limit
  • context
  • order -- optional order by specification, for overriding the natural sort ordering of the groups, see also search() (supported only for many2one fields currently)

Returns:

list of dictionaries(one dictionary for each record) containing:

  • groupby
  • __domain: list of tuples specifying the search criteria
  • groupby


Return type:

[{'field_name_1': value, ...]

Raises AccessError:

 

  • if user has no read rights on the requested object
  • if user tries to bypass access rules for read on the requested object


search ( cr,  user,  args,  offset=0,  limit=None,  order=None,  context=None,  count=False )


Search for records based on a search domain.

Parameters:

  • cr
  • user
  • args
  • offset
  • limit -- optional max number of records to return (default: None)
  • order
  • context (dictionary) -- optional context arguments, like lang, time zone
  • count -- optional (default: False), if True, returns only the number of records matching the criteria, not their ids

Returns:

id or list of ids of records matching the criteria

Return type:

integer or list of integers

Raises AccessError:

 

  • if user tries to bypass access rules for read on the requested object.


Expressing a search domain (args)

('field_name', 'operator', value), where:


  • field_name
  • operator must be a string with a valid comparison operator from this list: =,!=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_rightThe semantics of most of these operators are obvious. The child_ofoperator will look for records who are children or grand-children of a given record, according to the semantics of this model (i.e following the relationship field named by self._parent_name, by default parent_id.
  • value must be a valid value to compare with the values of field_name, depending on its type.


&' (logical AND, default), '|' (logical OR), '!' (logical NOT). These are prefix operators and the arity of the '&' and '|' operator is 2, while the arity of the '!' is just 1. Be very careful about this when you combine them the first time.

ABC



[('name','=','ABC'),'!',('language.code','=','en_US'),'|',('country_id.code','=','be'),('country_id.code','=','de'))



The '&' is omitted as it is the default, and of course we could have used '!=' for the language, but what this domain really represents is:



(name is 'ABC' AND (language is NOT english) AND (country is Belgium OR Germany))

unlink ( cr,  uid,  ids,  context=None )


Delete records with given ids

Parameters:

  • cr
  • uid
  • ids
  • context

Returns:

True

Raises:

  • AccessError
  • if user has no unlink rights on the requested object
  • if user tries to bypass access rules for unlink on the requested object
  • UserError

view_init ( cr,  uid,  fields_list,  context=None )


Override this method to do specific things when a view on the object is opened.

write ( cr,  user,  ids,  vals,  context=None )


Update records with given ids with the given field values

Parameters:

  • cr
  • user (integer) -- current user id
  • ids -- object id or list of object ids to update according to vals
  • vals (dictionary) -- field values to update, e.g {'field_name': new_field_value, ...}
  • context (dictionary) -- (optional) context arguments, e.g. {'lang': 'en_us', 'tz': 'UTC', ...}

Returns:

True

Raises:

  • AccessError
  • if user has no write rights on the requested object
  • if user tries to bypass access rules for write on the requested object
  • ValidateError
  • UserError

Note: The type of field values to pass in vals


•  For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics 
          

(0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
(1, ID, { values })    update the linked record with id = ID (write *values* on it)
(2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID)                cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID)                link to existing record with id = ID (adds a relationship)
(5)                    unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Example:
   [(6, 0, [8, 5, 6, 4])] sets the many2many to ids [8, 5, 6, 4]

•  For a one2many field, a lits of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics 
          

(0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
(1, ID, { values })    update the linked record with id = ID (write *values* on it)
(2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

Example:
   [(0, 0, {'field_name':field_value_record1, ...}), (0, 0, {'field_name':field_value_record2, ...})]

• False
• 'product.product, 5')