Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

关于系统的 _constraints 如何删除,或者继承?


  • 管理员

    例如 product/pricelist.py的

    <br />_constraints = [<br />&nbsp; &nbsp; &nbsp; &nbsp; (_check_date, &#039;You cannot have 2 pricelist versions that overlap!&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#91;&#039;date_start&#039;, &#039;date_end&#039;])<br />&nbsp; &nbsp; ]<br />
    


    如何继承或者删除呢。
    方案:
    1)直接改源码
    ...
    期待大家有更好的方案



  • 可能在自己的osv类中重载createInstance(cls, pool, module, cr)可能可以实现,从源码上看是在此处真正动态创建类的,且实例化的.我试了一下_sql_contraints,处理了父类继承过来的约束后,违反约束时出现的提示与父类不一样了。不过_constraints没有真正试过



  • 从createInstance(cls, pool, module, cr)源码看,为了删除现有的系统的_constraints,可以定义一个与要删除的_constrains同名的约束,且这个约束会一定满足就行。



  • [quote author=zjxplq link=topic=2587.msg8652#msg8652 date=1315671569]
    可能在自己的osv类中重载createInstance(cls, pool, module, cr)可能可以实现,从源码上看是在此处真正动态创建类的,且实例化的.我试了一下_sql_contraints,处理了父类继承过来的约束后,违反约束时出现的提示与父类不一样了。不过_constraints没有真正试过
    [/quote]


    也就是说 _sql_contraints 的继承实现可以覆盖基类的  _sql_contraints ?  测试过吗?
    Joshua 说测试 _contraints 未果...



  • [quote author=zjxplq link=topic=2587.msg8653#msg8653 date=1315705028]
    从createInstance(cls, pool, module, cr)源码看,为了删除现有的系统的_constraints,可以定义一个与要删除的_constrains同名的约束,且这个约束会一定满足就行。
    [/quote]

    顶这个!



  • 重载createInstance(cls, pool, module, cr)时,我测试了一下改变_sql_contraints,但改变不了natrr['_sql_contraints']的值,就为了这个搞了好长时间,后来不知怎样出现了提示信息与本来不一样了.太迟了就没有再试,就把设想发在网上了



  • 删除 _sql_contraints 我做过的
    <br / [检测到链接无效,已移除] br />
    Joshua现在碰到的问题是如何删除_contraints


  • 管理员

    这样怎么样:

    import ModuleName.ClassName<br />ModuleName.ClassName._constraints=&#91;]<br />
    


    Monkey Patch rocks, : )

    如果不想要这样的猴子戏法,在继承类中的_constraints里使用与父类的_constraints里的函数的名字相同即可覆写父类中的相应constraint.如果想删除对应的父类的constraint,只要在继承类中将对应的函数总是返回True即可。


  • 管理员

    经过查看5.x 和 6.x的代码我发现
    _constraints
    在5.x是有bug的不能继承的

    <br />if s==&#039;_constraints&#039;:<br />&nbsp; &nbsp; for c in cls.__dict__.get(s, &#91;]):<br />&nbsp; &nbsp; &nbsp; &nbsp; exist = False<br />&nbsp; &nbsp; &nbsp; &nbsp; for c2 in range(len(new)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #For _constraints, we should check field and methods as well<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if new[c2][2]==c[2] and new[c2][0]==c[0]:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[c2] = c<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exist = True<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<br />&nbsp; &nbsp; &nbsp; &nbsp; if not exist:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.append(c)<br />
    


    重点就在于

    <br />if new[c2][2]==c[2] and new[c2][0]==c[0]:<br />
    


    以上面的为例_constraints,这里分别是比较的是['date_start', 'date_end']和_check_date,但是它比较_check_date的时候不是比较名字,而是比较整个function的object,通过继承两个function是不可以相同的,所以应该这里应该是比较function name
    大家可以看看6.0就变成这样了

    <br />if s==&#039;_constraints&#039;:<br />&nbsp; &nbsp; for c in cls.__dict__.get(s, &#91;]):<br />&nbsp; &nbsp; &nbsp; &nbsp; exist = False<br />&nbsp; &nbsp; &nbsp; &nbsp; for c2 in range(len(new)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #For _constraints, we should check field and methods as well<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if new[c2][2]==c[2] and (new[c2][0] == c[0] \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or getattr(new[c2][0],&#039;__name__&#039;, True) == \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getattr(c[0],&#039;__name__&#039;, False)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # If new class defines a constraint with<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # same function name, we let it override<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # the old one.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[c2] = c<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exist = True<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<br />&nbsp; &nbsp; &nbsp; &nbsp; if not exist:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.append(c)<br />
    


    1.而校长说的写一个相同名字的函数名,但是这里直接调用类里的_check_date继承不了。
    2.猴子戏法可以实现把_constraints替换 😉
    有趣



  • 我在一个 模块中

    <br />...<br /><br />from openerp.addons import stock<br /><br />...<br />....<br /><br /><br />class stock_move(osv.osv):<br />....<br />&nbsp; &nbsp; _constraints = &#91;]<br /><br />&nbsp; &nbsp; for constraints in stock.stock_move._constraints:<br />&nbsp; &nbsp; &nbsp; &nbsp; if constraints[0].__name__ == &#039;_check_tracking&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br />&nbsp; &nbsp; &nbsp; &nbsp; _constraints.append(constraints)<br />&nbsp; &nbsp; stock.stock_move._constraints = _constraints<br /><br />...<br />
    




    然后 在某个业务操作后, 发现 还是调用了 openerp.addons.stock.stock_move._check_tracking 方法.
    是我没有理解猴子戏法?


  • 管理员

    试试不要放在 class stock_move(osv.osv)里面



  • [quote author=Joshua link=topic=2587.msg14383#msg14383 date=1362543836]
    试试不要放在 class stock_move(osv.osv)里面
    [/quote]

    <br /><br />...<br /><br />_constraints = &#91;]<br />for constraints in stock.stock_move._constraints:<br />&nbsp; &nbsp; if constraints[0].__name__ == &#039;_check_tracking&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; continue<br />&nbsp; &nbsp; _constraints.append(constraints)<br />stock.stock_move._constraints = _constraints<br /><br />class stock_move(osv.osv):<br /><br />&nbsp; &nbsp; _inherit = &#039;stock.move&#039;<br />&nbsp; &nbsp; _name = &#039;stock.move&#039;<br /><br />...<br />
    



    重启OE server 打断点的结果, 仍然 还是调用了 addons\stock\stock.py 里面的 _check_picking 方法.



  • 最新 消息 在 Joshua 和 buke 的耐心指导下 下面的代码测试在 6.1 下测试通过:

    <br /><br />...<br />class stock_move(osv.osv):<br /><br />&nbsp; &nbsp; _inherit = &#039;stock.move&#039;<br />&nbsp; &nbsp; _name = &#039;stock.move&#039;<br /><br />&nbsp; &nbsp; def __init__(self, pool, cr):<br />&nbsp; &nbsp; &nbsp; &nbsp; super(stock_move, self).__init__(pool, cr)<br />&nbsp; &nbsp; &nbsp; &nbsp; pool.models&#91;&#039;stock.move&#039;]._constraints = [x for x in pool.models&#91;&#039;stock.move&#039;]._constraints if x[0].__name__ != &#039;_check_tracking&#039;]<br /><br />...<br />
    



    代码作用: 移除 stock.move 对象 关于 _check_tracking 的约束...


Log in to reply