Odoo Help


Raise exception in an custom method

on 12/23/13, 7:03 AM 4,455 views
    _logger = logging.getLogger(__name__)

list_type = [
    ('full', 'Full Container'),
    ('loose', 'Loose Freight')

list_mode = [
    ('D2D', 'CY/CY - (Door to Door)'),
    ('D2P', 'CY/CY - (Door to Port)'),
    ('P2D', 'CY/CY - (Port to Door)'),
    ('P2P-1', 'CY/CY - (Port to Port)'),
    ('P2P-2', 'CFS/CY - (Port to Port)'),
    ('P2P-3', 'CFS/CFS - (Port to Port)'),
    ('other', 'Others')

list_container_size = [
    ('20"C', '20 Inch Container'),
    ('40"C', '40 Inch Container'),
    ('40"HQ', '40 Inch HQ Container'),
    ('40"R', '40 Inch Refer Container'),
    ('45"C', '45 Inch Container'),
    ('45"HQ', '45 Inch HQ Container')

list_transit_time = []
for i in range(1,365):
    if i == 1 :
        list_transit_time.append((i,str(i) + ' Day'))
        list_transit_time.append((i,str(i) + ' Days'))

#here generating the charges key which will be act as a unique key
def _generate_unique_key(self, cr, uid, ids, field_name, arg, context=None):
    result = {}
    for record in self.browse(cr, uid, ids, context=context):
        result[record.id] = str(record.port_lading_id.id) + '-' + str(record.port_unlading_id.id) + '-' + \
                            str(record.type) + '-' + str(record.mode) + '-' + str(record.via)
    return result

class shipment_charge(osv.osv):
    _name = 'shipment.charge'
    _columns = {
        'port_lading_id': fields.many2one('shipment.port', 'Port Lading', required=True, domain=[('port_lading', '=', 1)], store=True),
        'port_unlading_id': fields.many2one('shipment.port', 'Port Unlading', required=True, domain=[('port_unlading', '=' , 1)], store=True),
        'shipment_charge_price_ids': fields.one2many ('shipment.charge.price', 'shipment_charge_id', 'Container Quote'),
        'type': fields.selection(list_type, 'Type', required=True, help="Select Full Or loose Freight"),
        'mode': fields.selection(list_mode, 'Mode', required=True, help="Mode Of Container shipment"),
        'transit_time': fields.selection(list_transit_time, 'Transit Time', required=True, help="Transit Time In days"),
        'via':  fields.char('Via', size=64, help="Route , Via"),
        #two fields are missing have to update, agent_id and supplier_id
        'expiry_date': fields.date('Expiry Date',size=30, required=True, help='expiry date of this rates/charges after this date '
                                                               'charges ma be differ.'),
        'notes': fields.text('Notes', help='Notes Extra Information'),
        'charges_key':  fields.function(_generate_unique_key,type='char', method=True, store=True,
                                        string='charges_key', help="Charges key is a unique key for charges with "
                                                                   "combination of POL, POD, Agent, Supplier, Via, "
                                                                   "Mode separated with hyphens"),

    _sql_constraints = [
        ('charges_key_unique', 'unique (charges_key)','The Charges key is not Unique'),

    #here generating the charges key which will be act as a unique key
    def onchange_generate_unique_key(self, cr, uid, ids, port_lading_id, port_unlading_id):
        _logger.warning("IMPORT-LOG : in onchange function")
        charges_key = str(port_lading_id) + '-' + str (port_unlading_id)
        return {'value': {'charges_key': charges_key}}

Above is my module code , and i have a written custom function _generate_unique_key() which generates the unique charges as per

_sql_constraints = [
    ('charges_key_unique', 'unique (charges_key)','The Charges key is not Unique'),

but function freezes when it give _sql_constraints errors

error in log is IntegrityError: duplicate key value violates unique constraint "shipment_charge_charges_key_unique" DETAIL: Key (charges_key)=(1-3-full-D2D-Dir) already exists.

please suggest how to raise error when it counters the duplicate value

Thanks in advance

On 12/23/13, 9:52 AM

Hi there,

If you want to show an alert or popup for Error/Warning, then you may try the following :

-> from openerp.tools.translate import _ (at the beginning of the file)

-> then you for validating unique constraint you can define your custom method and pass the value to it.

-> for alert/popup when error occurs, you can use :

raise osv.except_osv(_("Warning"), _("My warning Msg"))

or, if you want an error popup try this : raise osv.except_osv(_("Error!"), _("My error Msg"))

Please check this one and update me if this is useful for you or not, and if you want I can share a code snippet of mine where I've referred to a custom validation method for doing operation upon specific condition. (if.....else)



Thanks but how can i use this with

'charges_key': fields.function(_generate_unique_key,type='char', method=True, store=True,

on 12/23/13, 9:57 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 12/23/13, 7:03 AM
Seen: 4455 times
Last updated: 3/16/15, 8:10 AM