Odoo Help

Welcome!

This community 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.

4

how to make a field unique with combination of two fields in the same table

By
Soohoo
on 12/24/13, 4:00 AM 2,711 views

I want to make a field Unique with combination of two fields and it does not show on form

class shipment_charge(osv.osv):
    _name = 'shipment.charge'
    _columns = {
        'type': fields.selection(list_type, 'Type', required=True, ),
        'mode': fields.selection(list_mode, 'Mode', required=True, help=""),
        'transit_time': fields.selection(list_transit_time, 'Transit Time', required=True, help=""),
        'charges_key':  fields.char('charges_key', size=64, help=""),
    }

    _sql_constraints = [
        ('charges_key_unique', 'unique (type, mode)', 'The Charges key is not Unique')
    ]
3
Ashif Abdulrahman
On 12/24/13, 5:19 AM

use this

def check_existance(self, cr, uid, ids, context=None):
        self_obj = self.browse(cr, uid, ids[0], context=context)
        field1 = self_obj.field1
        field2 = self_obj.field2
        search_ids = self.search(cr, uid, [('field1', '=', field1),('field2', '=' , field2)], context=context)
        res = True
        if len(search_ids) > 1:
            res = False
        return res

and add

_constraints = [(check_existance,'Combination already exist', ['field1'])]

still giving error ValueError: need more than 1 value to unpack

Soohoo
on 12/24/13, 6:24 AM

use

_constraints = [(check_existance,'Combination already exist', ['field1','field2'])]

Ashif Abdulrahman
on 12/24/13, 7:14 AM
1
Abhishek H Menon
On 12/24/13, 5:04 AM
_sql_constraints = [
        ('name_uniq', 'unique(type, mode)', 'The Charges Key is not Unique!'),
        ]

Please replace this instead of that

ERROR erp_dev openerp.netsvc: Constraint Error The Charges key is not Unique

its freezing the screen not rasing a error message

Soohoo
on 12/24/13, 5:15 AM
0
Thomas
On 11/8/16, 6:42 PM

enclose the code of a server action which concatenetes 2 or more fiel into a new one

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 12/24/13, 4:00 AM
Seen: 2711 times
Last updated: 11/8/16, 6:42 PM