OpenERP的Object的若干重要方法备忘
-
关于OpenERP的Object,先贴上网上收集到的一点资料,等有空了再展开研究下。
记得以前有人问过,怎么动态修改视图上字段的显示标签,看完本帖,重载Object的方法fields_view_get ,就可以任意动态修改字段标签了。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..class osv.osv.osv(pool, cr)
browse(cr, uid, select, context=None, list_class=None, fields_process={})
Fetch records as objects allowing to use dot notation to browse fields and relations
Parameters:
* cr – database cursor
* user – current user id
* select – id or list of ids
* context – context arguments, like lang, time zone
Return type:
object or list of objects requestedcheck_access_rule(cr, uid, ids, operation, context=None) Verifies that the operation given by operation is allowed for the user according to ir.rules. Parameter: operation – one of write, unlink Raises except_orm: * if current ir.rules do not permit this operation. Returns: None if the operation is allowed check_recursion(cr, uid, ids, parent=None) Check recursion in records Parameters: * cr – database cursor * uid – current user id * ids – list of ids of records * parent – parent field name Returns: True or False based on recursion detection copy(cr, uid, id, default=None, context=None) Duplicate record with given id updating it with default values Parameters: * cr – database cursor * uid – current user id * id – id of the record to copy * default (dictionary (example {‘field_name’: field_value, ...})) – dictionary of field values to update before saving the duplicate object * context – 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 – database cursor * user – current user id * ids – id of the record to copy * default – dictionary of field values to update before saving the duplicate object * context – 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 – database cursor * user (integer (example 1)) – current user id * vals (dictionary (example {‘field_name’: field_value, ...})) – dictionary for new record {‘field_name’: field_value, ...} * context(optional, highly recommended) – context arguments, like lang, time zone 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 Raises ValidateError: if user tries to enter invalid value for a field that is not in selection vals format for relational field type. * many2many field : [(6, 0, list of ids)] (example: [(6, 0, [8, 5, 6, 4])]) * one2many field : [(0, 0, dictionary of values)] (example: [(0, 0, {‘field_name’:field_value, ...})]) * many2one field : ID of related record * reference field : model name, id (example: ‘product.product, 5’) default_get(cr, uid, fields_list, context=None) To Get default field values of given fields list of the model Parameters: * cr – database cursor * uid – current user id * fields_list (list (example [‘field1’, ‘field2’,])) – list of fields to get the default value * context – context arguments, like lang, time zone Returns: dictionary of the default values for fields (set on the object class, by the user preferences, or via the context) export_data(cr, uid, ids, fields_to_export, context=None) Export fields for selected objects Parameters: * cr – database cursor * uid – current user id * ids – list of ids * fields_to_export – list of fields * context – context arguments, like lang, time zone, may contain import_comp(default: False) to make exported data compatible with import_data() Return type: dictionary with a datas matrix 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 – database cursor * user – current user id * fields – list of fields * context – context arguments, like lang, time zone 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 – database cursor * user – current user id * view_id – id of the view or None * view_type – type of the view to return if view_id is None (‘form’, tree’, ...) * context – context arguments, like lang, time zone * toolbar – true to include contextual actions * submenu – example (portal_project module) 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 Raises Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure import_data(cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None) Import given data in given module Parameters: * cr – database cursor * uid – current user id * ids – list of ids * fields – list of fields * data – data to import * mode – ‘init’ or ‘update’ for record creation * current_module – module name * noupdate – flag for record creation * context – context arguments, like lang, time zone, * filename – optional file to store partial import state for recovery Return type: tuple This method is used when importing data via client menu name_get(cr, user, ids, context=None) Parameters: * cr – database cursor * user (integer (example 1)) – current user id * ids – list of ids * context – 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)¶ Parameters: * cr – database cursor * user – current user id * name – object name to search * args – list of tuples specifying search criteria [(‘field_name’, ‘operator’, ‘value’), ...] * operator – operator for search criterion * context – context arguments, like lang, time zone * limit – optional max number of records to return 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() perm_read(cr, user, ids, context=None, details=True) Read the permission for record of the given ids Parameters: * cr – database cursor * user – current user id * ids – id or list of ids * context – context arguments, like lang, time zone * details – if True, *_uid fields are replaced with the name of the user 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 read(cr, user, ids, fields=None, context=None, load='_classic_read') Read records with given ids with the given fields Parameters: * cr – database cursor * user – current user id * ids – id or list of the ids of the records to read * fields (list (example [‘field_name_1’, ...])) – optional list of field names to return (default: all fields would be returned) * context(optional, highly recommended) – context arguments, like lang, time zone Returns: list of dictionaries((dictionary per record asked)) with requested field values Return type: [{‘name_of_the_field’: 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 read_group(cr, uid, domain, fields, groupby, offset=0, limit=None, context=None) Get the list of records in list view grouped by the given groupby fields Parameters: * cr – database cursor * uid – current user id * domain – list specifying search criteria [[‘field_name’, ‘operator’, ‘value’], ...] * fields – list of fields present in the list view specified on the object * groupby – list of fields on which to groupby the records * offset – optional number of records to skip * limit – optional max number of records to return * context – context arguments, like lang, time zone Returns: list of dictionaries(one dictionary for each record) containing: * the values of fields grouped by the fields in groupby argument * __domain: list of tuples specifying the search criteria * __context: dictionary with argument like 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 record/s with or without domain Parameters: * cr – database cursor * user – current user id * args – list of tuples specifying search criteria [(‘field_name’, ‘operator’, ‘value’), ...] * offset – optional number from search starts * limit – optional max number of records to return * order – optional columns to sort by (default: self._order=id ) * context(optional, highly recommended) – context arguments, like lang, time zone * count – 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. Operators: * =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right Prefix operators: * ‘&’ (default), ‘|’, ‘!’ unlink(cr, uid, ids, context=None) Delete records with given ids Parameters: * cr – database cursor * uid – current user id * ids – id or list of ids * context(optional, highly recommended) – context arguments, like lang, time zone 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 Raises UserError: if the record is default property for other records 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 – database cursor * user (integer (example 1)) – current user id * ids – id or list of ids * vals (dictionary (example {‘field_name’: ‘value’, ...})) – dictionary of field values to update * context(optional, highly recommended) – context arguments, like lang, time zone 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 Raises ValidateError: if user tries to enter invalid value for a field that is not in selection Raises UserError: if recurssion is found vals format for relational field type. * many2many field : [(6, 0, list of ids)] (example: [(6, 0, [8, 5, 6, 4])]) * one2many field : [(0, 0, dictionary of values)] (example: [(0, 0, {‘field_name’:field_value, ...})]) * many2one field : ID of related record * reference field : model name, id (example: ‘product.product, 5’)