Skip to content

Instantly share code, notes, and snippets.

@tejastank
Created July 12, 2012 20:14
Show Gist options
  • Save tejastank/3100658 to your computer and use it in GitHub Desktop.
Save tejastank/3100658 to your computer and use it in GitHub Desktop.
OpenERP-Module-Model.py File
# -*- coding: utf-8 -*-
# First import python library or package or files
import smtplib
# Then import openerp related packages or libs
from osv import fields, osv
import addons
class model_name(osv.osv):
def name_get(self, cr, uid, ids, context=None):
if not ids:
return []
reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
res = []
# name_get: method return list
return res
def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
res = self.name_get(cr, uid, ids, context=context)
return dict(res)
_name = "model.name"
_description = "Describe model in very short details."
_columns = {
'name': fields.char("Category", size=64, required=True),
'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Name'),
'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
}
def _check_recursion(self, cr, uid, ids, context=None):
# VERY USEFUL CODE TO TEST RECURSION IN PARENT-CHILD RELATIONS
level = 100
while len(ids):
cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
ids = filter(None, map(lambda x:x[0], cr.fetchall()))
if not level:
return False
level -= 1
return True
_constraints = [
(_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
]
model_name()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment