orm - Use a model's field as an identifier -
orm - Use a model's field as an identifier -
i have next model in openerp 7. how indicate isbn field should primary key.
from osv import osv, fields class book(osv.model): """ book """ _name = 'helloworld.book' _columns = { 'isbn' : fields.char('isbn', size=9, requried=true), 'title' : fields.char('title', size=100, required=true), 'genre' : fields.char('genre', size=20, required=true), }
a primary key special relational database table column (or combination of columns) designated uniquely identify table records.
a primary key’s main features are:
it must contain unique value each row of data.
it cannot contain null values.
a primary key either existing table column or column generated database according defined sequence.
openerp supports keeping unique sql constraint on field.
_sql_constraints = [ ('my_key_value', 'unique(key_value1,key_value2)', 'key value has unique !') ] in case, have chosen char type of field, set sql constraint this.add class. , restart server , check again. find primary key's functionality on 'isbn' field.
_sql_constraints = [ ('isbn_uniq', 'unique(isbn)', 'isbn must unique!'), ] for case insensitive constraints,
def _check_unique_insesitive(self, cr, uid, ids, context=none): sr_ids = self.search(cr, uid ,[], context=context) lst = [x.ibsn.lower() x in self.browse(cr, uid, sr_ids, context=context) if x.ibsn] self_obj in self.browse(cr, uid, ids, context=context): if self_obj.ibsn , self_obj.ibsn.lower() in lst: homecoming false homecoming true _constraints = [(_check_unique_insesitive, 'error: isbn must unique!', ['ibsn'])] hope help you. :)
orm openerp-7
Comments
Post a Comment