All the necessary files needed for creating the module and connecting with the view lies under the base module. Base module contain a folder called osv in which all the functions and classes needed for creating a model is defined here. So first of all Import fields and osv from osv.
To define a new object, you have to define a new Python class then instantiate it. This class must inherit from the osv class in the osv module.The first line of the object definition will always be of the form:
An object is defined by declaring some fields with predefined names in the class. Two of them are required (_name and _columns), the rest are optional. The predefined fields are:
- _auto
- _columns (required)
- _constraints
- _sql_constraints
- _defaults
- _inherit
- _inherits
- _log_access
- _name (required)
- _order
- _rec_name
- _sequence
- _sql
- _table
In the database, tables will be created with the table name given in the _name field (all the dot"." will be replaced by underscore"_" in table creation)
Fields for the database table is specified inside the "_columns". Columns is an associative array(dictionary) in which we specify the field name as the key and field type as the value.
Types of fields
Basic types
boolean: | A boolean (true, false). Syntax:fields.boolean('Field Name' [, Optional Parameters]), |
---|---|
integer: | An integer. Syntax:fields.integer('Field Name' [, Optional Parameters]), |
float: | A floating point number. Syntax:fields.float('Field Name' [, Optional Parameters]),
|
char: | A string of limited length. The required size parameter determines its size. Syntax:fields.char(
'Field Name',
size=n [,
Optional Parameters]), # where ''n'' is an integer. |
text: | A text field with no limit in length. Syntax:fields.text('Field Name' [, Optional Parameters]), |
date: | A date. Syntax:fields.date('Field Name' [, Optional Parameters]), |
datetime: | Allows to store a date and the time of day in the same field. Syntax:fields.datetime('Field Name' [, Optional Parameters]), |
binary: | A binary chain. We can import(upload) our files using binary. |
selection: | A field which allows the user to make a selection between various predefined values. Syntax:fields.selection((('n','Unconfirmed'), ('c','Confirmed')),
'Field Name' [, Optional Parameters]), |
- one2many and many2one
- An example is relationship between employee and department. A department may have so many employees and employees will belong to only one department.
- When defining a many2one relationship in open erp, at first there must have a one2many relationship. ie the constructor which have the one2many relation must be called before calling the constructor having many2one relation.
- {"employee":fields.one2many("employee.class","department_id","Employees")}
In class department we define the field as dictionary values, “employee” as the key, “fields.one2many()” as the value with 3parameters. First parameter is the other class name(here employee.class ), then the key name of the other class which has the many2one as its value(here employee_id), then the field name.
- {"department_id":fields.many2one("department.class","Department"),}
In class employee we define the field as dictionary values, “department_id” as the key, “field.many2one()” as the value with 2parameters. First parameter is the other class name(here is the department.class) then the field name.
- many2many
- Example is the relationship between user and group. Users may belong to so many groups and a group may have so many users.
- For showing a many2many relationship, we have to create a new relationship table in the database with fields user_id and group_id.
- If we want a many2many relation between two classes say class abc and class xyz then we have to first define both classes. Now add the many2many relationship in the class which secondly created( here class xyz). Now define a new class which inherit the properties of the class abc. Define the many2many relationship here in the inherited class.
- { 'user_ids': fields.many2many('group.class','user_group_rel', 'user_id','group_id','Users')}
- The parameters are
- other class name
- relationship table name
- my_relation_id
- other_relation_id
- field name
No comments:
Post a Comment