-1

请在视图表单中我希望使用字段作为键,并且每条记录必须是唯一的才能使用它来浏览其他视图中的表,怎么做

    class saisirsoum(osv.osv):
        _name='saisir.soum' 

        _columns = {
        'NumOffre' : fields.char('N° Offre'), # to be defined as key !!
        'organisme_s' : fields.char('Organisme'),
        'des_offre' : fields.char('Designation'),
        'order_line' :fields.one2many('saisir.soumission.ligne','order_id','soumission_id'),
        'observation_d' : fields.text('Observation'),
    }
4

1 回答 1

0

在您的情况下,您将NumOffre通过设置_sql_constraints变量来做到这一点。您还可以在其中定义在用户尝试添加重复条目时显示的自定义消息。

class saisirsoum(osv.osv):

    _name='saisir.soum'

    _sql_constraints = [
    ('NumOffre', 'unique(NumOffre)', 'NumOffre already exists'),
    ]

    _columns = {
    'NumOffre' : fields.char('N° Offre'), # to be defined as key !!
    'organisme_s' : fields.char('Organisme'),
    'des_offre' : fields.char('Designation'),
    'order_line' :fields.one2many('saisir.soumission.ligne','order_id','soumission_id'),
    'observation_d' : fields.text('Observation'),
    }


    def create(self, cr, uid, vals, context=None): # when the record is about to created
        NumOffre = vals.get('NumOffre') #get NumOffre from the form
        if NumOffre:
            pass # you can do something with it e.g searching

        return super(saisirsoum, self).create(cr, uid, vals, context=context) # finally call the create method from the super class and create the record after you're done

您还可以覆盖其他 CRUD 方法,例如writeunlink以相同的方式

于 2016-04-22T00:22:52.060 回答