Openerp objects support the following fields: Char, text, Boolean, integer, float, date, time, datetime, and binary. complex types: selection, function, and related; link Type: one2one, one2133, many2one, and many2133. The following sections describe them one by one.
Boolean: Boolean (true, false)
Integer: integer.
Float: Float type, such as 'rate': fields. Float ('relative change rate', digits = (). digits defines the digits of the integer part and the decimal part.
Char: String length. The size attribute defines the string length.
Text: Text type, no length limit.
Date: Date type
Datetime: Date and Time Type
Binary: Binary
Function: Function type. The field value of this type is calculated by the function and is not stored in the data table. Its definition format is:
Fields. function (fnct, Arg = none, fnct_inv = none, fnct_inv_arg = none, type = 'float', fnct_search = none, OBJ = none, method = false, store = true)
· Type is the type of the function return value.
· If method is true, the function of this field is a method of the object. If it is false, it indicates a global function, not an object method. If method = true, OBJ specifies the object of method.
· Fcnt is a function or method used to calculate the field value. If method = true, fcnt is the object method. The format is def fnct (self, Cr, uid, IDs, field_name, argS, context). Otherwise, the format is as follows: def fnct (Cr, table, IDs, field_name, argS, context ). IDS is the current access record ID passed in by the system. Field_name is the name of the current field. When a function is used for multiple function field types, this parameter can differentiate fields. ARGs is a parameter passed in by 'arg = none.
· Fcnt_inv is a function or method used to write this field. If method = true, the format is def fcnt_inv (self, Cr, uid, IDs, field_name, field_value, argS, context). Otherwise, the format is def fcnt_inv (Cr, table, IDs, field_name, field_value, argS, context)
· Fcnt_search defines the search behavior of this field. If method = true, the format is def fcnt_search (self, Cr, uid, OBJ, field_name, argS). Otherwise, the format is def fcnt_search (Cr, uid, OBJ, field_name, ARGs)
· Store indicates whether to store the field value in the database. The default value is false. However, store also has an enhancement form in the format of store = {'object _ name' :( function_name, ['field _ name1', 'field _ name2'], priority )}, it means that if the field ['field _ name1 ', 'field _ name2'] of the object 'object _ name' changes, the system calls the function_name function, the return result of the function is transmitted to the main function of this field as the parameter (ARG), that is, fnct.
Selection: Drop-down box field. Defines a drop-down box that allows users to select a value. For example, 'state': fields. selection ('n', 'unfirmed '), ('C', 'firmed'), 'state', required = true ), this indicates that the State field has two options ('n', 'unfirmed ') and ('C', 'firmed ').
One2one: One-to-one relationship in the format of fields. one2one (associated Object Name, field display name ,...). In Versions later than V5.0, we do not recommend using many2one instead.
Many2one: Multi-to-one relationship, in the format of fields. many2one (Association Object Name, field display name ,...). Optional parameters: ondelete; optional values: "cascade" and "null"; default value: "null", indicating whether the record of the deleted end is Cascade deleted after the record of one end is deleted.
One2workflow: One-to-many relationship in the format of fields. one2second (associated Object Name, associated field, field display name ,...), for example, 'address': fields. one2second ('res. partner. address ', 'partner _ id', 'contacts ').
Many2many: Many-to-many relationship. For example:
'Category _ id': fields. many2res ('res. partner. CATEGORY ', 'res _ partner_category_rel', 'partner _ id', 'category _ id', 'categories '),
This parameter indicates that the object res. Partner. category is associated with many-to-many relationships. The association table is 'res _ partner_category_rel ', and the association fields are 'partner _ id' and 'category _ id '. When the preceding fields are defined, openerp automatically creates the Association Table 'res _ partner_category_rel ', which contains the association fields 'partner _ id' and 'category _ id '.
Reference: Reference type, in the format of fields. Reference (field name, selection, size ,...). Here, selection is: 1) the function that returns the tuple list, or 2) the tuples list of the object (or model) referenced by this field. The reference field is stored in the database table in the form of (Object Name, ID), such as (product. product, 3) indicates the referenced object product. id = 3 in product (data table product_product. Example of reference:
Def _ links_get (self, Cr, UID ):
Cr.exe cute ('select object, name from res_request_link order by priority ')
Return Cr. fetchall ()
...
'Ref ': fields. Reference ('document ref 2', selection = _ links_get, size = 128 ),
...
In the preceding example, the object type resource that can be referenced by the field ref is selected from the drop-down list. The options in the drop-down box are returned by function _ links_get, which is a list of (object, name) pairs, such as [("product. product "," product "), (" account. invoice "," invoice "), (" stock. production. lot "," production lot ")].
Related: Joined field, indicating that this field references a field in the joined table. Format: fields. related (relational field, reference field, type, relation, String ,...), A link field is a field of the object (usually one2pipeline or many2pipeline). The reference field is the field of the data table associated with the link field, and the type is the type of the reference field, if the type is many2one or many2many, relation indicates the joined table. Example:
'Address': fields. one2second ('res. Partner. address', 'partner _ id', 'contacts '),
'City': fields. Related ('address', 'city', type = 'Char ', string = 'city '),
'Country': fields. Related ('address', 'country _ id', type = 'many2one', relation = 'res. country', string = 'country '),
Here, city references the City field of address, and country references the country object of address. In the associated object res. Partner. Address of address, country_id is a field of the many2one type, so type = 'many2one', relation = 'res. Country '.
Property: Property field. The following is an example of the property field type.
'Property _ product_pricelist': fields. property ('product. pricelist ', type = 'manyun2one', relation = 'product. pricelist ', string = "Sale pricelist", method = true, view_load = true, group_name = "pricelists properties ")
In this example, the object is associated with the object product. pricelist (relation = 'product. pricelist') through the 'Property _ product_pricelist 'many-to-one (type = 'many2one') field '). Different from the many2one field type, the many2one field creates the data table field 'Property _ product_pricelist 'In this object, and the property field type does not create the data table field 'Property _ product_pricelist '. The property field type is retrieved from the data table IR. search for name = 'Property _ product_pricelist' in property (that is, 'product. pricelist 'is prefixed with property and ". "Replace with" _ "as name) and company_id is the same as the record of this object, and query the association record from the value field of this record (value field type is reference), such as (product. pricelist, 1) indicates that the resource of this object is associated with the product of the object. pricelist id = 1. That is to say, the property field type is indirectly correlated to another object through IR. Property.
The property field type is basically the same as the many2one field type, but the two conditions are better than the many2one field. One is, for example, when multiple records pass through IR. the property name = 'Property _ product_pricelist' record is associated with the record (product. pricelist, 1). In this case, if you want to change all the associations to the associated records (product. pricelist, 2 ). If it is a many2one type, it is difficult to complete this task without writing code. If it is a property field, you only need to set IR. value (product. pricelist, 1) to (product. pricelist, 2), then all the associations have changed. You can modify the value of IR. Property in the menu configuration> properties under system management. The second is, for example, the price list of the business partner that a user wants to see is pricelista, and the price list of the business partner is pricelistb, the many2one type cannot achieve this effect. The property type adds the company_id condition when associating records in IR. property, so that employees of different companies can see different associated records.
Because the property type is associated with IR. property, each property field must have an association record in IR. Property. This record can be imported during installation. The reference code is as follows:
<Record model = "ir. Property" id = "property_product_pricelist">
<Field name = "name"> property_product_pricelist </field>
<Field name = "fields_id" Search = "[('model', '=', 'res. partner '), ('name',' = ', 'Property _ product_pricelist')] "/>
<Field name = "value" EVAL = "'product. pricelist, '+ STR (list0)"/>
</Record>
Parameter defined by field
Available parameters in the field definition include change_default, readonly, required, States, String, translate, size, priority, domain, invisible, context, and selection.
Change_default: Can the default values of other fields depend on this field? The default value is false. Example (see res. Partner. Address ),
'Zip': fields. Char ('zip', change_default = true, size = 24 ),
In this example, you can set the default value of other fields based on the zip value. For example, you can use the program code. If zip is 200000, the city is set to "Shanghai ", if zip is 100000, the city is "Beijing ".
Readonly: Whether the field is read-only. Default Value: false.
Required: Required or not. The default value is false.
States: Defines the attribute that takes effect for a specific state. The format is {'name _ of_the_state ': list_of_attributes}. The list_of_attributes is like [('name _ of_attribute ', value),...] tuples list. Example (see account. Transfer ):
'Partner _ id': fields. many2one ('res. partner', 'partner', States = {'posted': [('readonly', true)]}),
String: Field display name, any string.
Translate: Whether the field value (not the display name of the Field) can be translated. Default Value: false.
Size: Field length.
Priority:
Domain: Domain condition. Default Value: []. In the many2dataset and many2one types, the field value is the ID of the associated table, and the field condition is used to filter the record of the associated table. Example:
'Default _ credit_account_id ': fields. many2one ('account. account', 'default credit account', domain = "[('type ','! = ', 'View')] "),
In this example, this field is associated with the record of the object ('account. account') and the type is not 'view.
Invisible: Whether this field is visible, that is, whether to display this field on the interface. The default value is true.
Selection: Used only for the reference field type. See the previous reference description.
> Another problem is whether the function store = true makes sense. Instead of reading the function every time, it executes the method. What is the role of storing the function in the database?
My understanding is that store = true can store the results of each function compute to the database, which has two advantages: one is to facilitate DBA to view data from the database; second, non-openerp software can directly access the database to share data. In addition, fcnt_inv is not limited to data tables written to this object. More often, it is written to other objects, or even data tables of multiple objects at the same time.
> <Field name = "value" EVAL = "'product. pricelist, '+ STR (list0)"/>
As mentioned above, the value field type is reference, that is, the value in the form of (product. pricelist, 1. This line is the value of the calculated value, such as (product. pricelist, list0), list0 (the value after import is a number) is the referenced product. the ID of a record in pricelist.
<Record ID = "list0" model = "product. pricelist">
<Field name = "name"> default purchase pricelist </field>
<Field name = "type"> purchase </field>
</Record>
That is, in <field name = "value" EVAL = "'product. before pricelist, '+ STR (list0) "/>, you must first have the code line with the import id =" list0.
Each openerp object has predefined methods defined in the base class OSV. OSV. These predefined methods include:
Basic Method: Create, search, read, browse, write, unlink.
Def create (self, Cr, uid, Vals, context = {})
Def search (self, Cr, uid, argS, offset = 0, limit = 2000)
Def read (self, Cr, uid, IDs, fields = none, context = {})
Def browse (self, Cr, uid, select, offset = 0, limit = 2000)
Def write (self, Cr, uid, IDs, Vals, context = {})
Def unlink (self, Cr, uid, IDS)
Default Value access method: Default_get, default_set.
Def default_get (self, Cr, uid, fields, form = none, reference = none)
Def default_set (self, Cr, uid, field, value, for_user = false)
Special Field Operation Method: Perm_read, perm_write
Def perm_read (self, Cr, uid, IDS)
Def perm_write (self, Cr, uid, IDs, fields)
How to operate fields and views: Fields_get, distinct_field_get, fields_view_get
Def fields_get (self, Cr, uid, fields = none, context = {})
Def fields_view_get (self, Cr, uid, view_id = none, view_type = 'form', context = {})
Def distinct_field_get (self, Cr, uid, field, value, argS = [], offset = 0, limit = 2000)
Record Name access method: Name_get, name_search
Def name_get (self, Cr, uid, IDs, context = {})
Def name_search (self, Cr, uid, name = '', argS = [], operator = 'ilike', context = {})
Default Value access method: Default_get, default_set
Def name_get (self, Cr, uid, IDs, context = {})
Def name_search (self, Cr, uid, name =, argS = [], operator = 'ilike', context = {})
Create method: Insert a record in the data table (or create an object resource ).
Format: def create (self, Cr, uid, Vals, context = {})
Parameter description:
Vals: The field value of the record to be created. It is a dictionary, for example, {'name _ of_the_field ': value ,...}
Context (optional): Almost all openerp Methods Carry the context parameter. context is a dictionary that stores some context values, such as the information of the current user, including the language and role. Context can be inserted with any value. In the action definition, there is a context attribute. During interface definition, any value can be placed in this attribute. The initial value of context is usually from this attribute value.
Return Value: the ID of the new record.
Example: Id = pooler. get_pool (Cr. dbname ). get ('res. partner. event '). create (Cr, uid, {'name': 'email sent through mass mailing ', 'partner _ id': Partner. ID, 'description': 'The description for partner event '})
Search Method: query the matching records.
Format: def search (self, Cr, uid, argS, offset = 0, limit = 2000)
Parameter description:
ARGs: List of tuples that contain search conditions. Format: [('name _ of_the_field ', 'operator', value),...]. Available operators include:
=,>, <, <=,> =
In
Like, Ilike
Child_of
For more details, refer to the "Domain conditions" section in "openerp Application and Development basics.
· Offset (optional): number of offset records, indicating that the first offset record of the search result is not returned.
· Limit (optional): Maximum number of records returned.
Returned value: the ID list of records that meet the conditions.
Read method: return the list of specified field values of the record.
Format: def read (self, Cr, uid, IDs, fields = none, context = {})
Parameter description:
· IDs: List of IDs of the records to be read, for example, [1, 3, 5,...]
· Fields (optionnal): the value of the field to be read. If this parameter is not specified, all fields are read.
· Context (optional): See the create method.
Returned value: the dictionary list of the read results, such as [{'name _ of_the_field ': value,...},...]
Browse method: browse objects and their associated objects. Read the specified record from the database and generate an object to return the record. Unlike methods such as read, this method does not return a simple record, but returns an object. You can directly use ". "Access Object fields and methods, such as" object. name_of_the_field ", associated fields (many2one, etc.), you can also directly access the" adjacent "object through the associated fields. For example:
Addr_obj = self. Pool. Get ('res. Partner. address'). Browse (Cr, uid, contact_id)
Nom = addr_obj.name
Compte = addr_obj.partner_id.bank
This code first retrieves the object res from the object pool. partner. address, call its method browse, get the Object ID = contact_id, and then directly use ". "obtain the" name "field and the Bank of the associated object patner (addr_obj.partner_id.bank ).
Format: def browse (self, Cr, uid, select, offset = 0, limit = 2000)
Parameter description:
Select: ID of the object to be returned, which can be an ID or an ID list.
· Offset (optional): See the search method.
· Limit (optional): See the search method.
Return Value: returns an object or a list of objects.
Note: This method can only be used on the server. Remote calls such as RPC are not supported due to efficiency and other reasons.
Write method: stores one or several fields of a record.
Format: def write (self, Cr, uid, IDs, Vals, context = {})
Parameter description:
· IDs: List of IDs of records to be modified.
· Vals: the new value of the field to be saved. It is a dictionary, for example, {'name _ of_the_field ': value ,...}.
· Context (optional): See the create method.
Return Value: If no exception exists, true is returned; otherwise, an exception is thrown.
Example: Self. Pool. Get ('sale. Order'). Write (Cr, uid, IDs, {'state': 'cancel '})
Unlink method: delete one or more records.
Format: def unlink (self, Cr, uid, IDS)
Parameter description:
· IDs: List of IDs of records to be deleted.
Return Value: If no exception exists, true is returned; otherwise, an exception is thrown.
Default_get method: reset the default value of one or more fields.
Format: def default_get (self, Cr, uid, fields, form = none, reference = none)
Parameter description:
? Fields: List of fields for which you want to reset the default value.
? Form (optional): it does not seem to be used currently (version 5.06 ).
? Reference (optional): Currently it does not seem to be used (version 5.06 ).
Returned value: the default value of the field, which is a dictionary, for example, {'field _ name': value ,...}.
Example: Self. Pool. Get ('hr. Analytic. timesheet '). default_get (Cr, uid, ['product _ id', 'product _ uom_id'])
Default_set method: reset the default value of the field.
Format: def default_set (self, Cr, uid, field, value, for_user = false)
Parameter description:
? Field: The field of the default value to be modified.
? Value: The new default value.
? For_user (optional): whether the modification is valid only for the current user or all users. The default value is valid for all users.
Return Value: True