Introduction



2 files need to be modified when we creating a custom model
  1. _ _init_ _.py
  2. <model name>.py

model python file



all of the <model name>,py require to inherit osv. Eg

from osv import osv, fields
class Student(osv.osv):
...._name = "college.student"
...._description = "Student Model File"
...._columns = {
..........'fname' : fields.char('Name'),
..........'age' : fields.integer('Age'),
..........'dob' : fields.date('Date of Birth'),
..........'department_id' : fields.many2one('college.department','Department'),
..........'married' : fields.boolean('Married'),
..........'gender' :fields.selection([('male','Male'),('female','Female')],'Gender'),
....}
Student()

_ _init_ _.py



You need to import your custom model python file in the init python file to make the OpenERP take effect. Eg.
import college


Fields available for Model


Type of fields
Example
fields.one2many( 'other.object.name', 'Field relation id', 'Fieldname', optional parameter)
fields.one2many('college.student','department_id','Students')
fields.many2one( 'other.object.name', 'Field Name', optional parameters)
fields.many2one('college.department','Department')
fields.many2many('other.object.name', 'relation object', 'actual.object.id', 'other.object.id', 'Field Name')
fields.many2many(
'college.teacher', # Model Name
'student_teacher_rel', # Relation name
'student_id', # student foreign key in relation table
'teacher_id', # teacher foreign key in relation table
'Teachers' # field name
)
fields.integer('Field Name' [, Optional Parameters])
fields.integer('Age')
fields.char('Field Name', size=n [, Optional Parameters])
fields.char('Name')

Optional Parameters


Name
Description
readonly
True if the user cannot edit this field, otherwise false
required
True if this field must have a value before the object can be saved, else false
size
The size of the field in the database, number of characters or digits
states
Lets you override other parameters for specific states of this object. Accepts a dictionary with the state names as keys and a list of name/value tuples as the values. For example: states={'posted':[('readonly',True)]}

*More optional parameters and fields can be found in https://doc.openerp.com/trunk/server/03_module_dev_02/

Tips



  1. Any modification on the model require to restart the server