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

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

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

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

求助这段fnct_search代码,帮忙解释一下!



  • [size=18pt]我想对一个function字段进行搜索,经过大家指点,知道了这个方法!只是本人语言基础薄弱!
    想请大家指点一下!

    def _invoice_search(self, cursor, user, obj, name, args): # 这里是定义这个搜索的功能!<br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(args):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # args 应该是指我们搜索的时候输入的条件<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; invoice_obj = self.pool.get(&#039;account.invoice&#039;) # 这里定义了将要搜索用到的 object<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; i = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 这一段是我不明白的地方!<br />&nbsp; &nbsp; &nbsp; &nbsp; while i &lt; len(args):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fargs = args[i][0].split(&#039;.&#039;, 1)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(fargs) &gt; 1:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i] = (fargs[0], &#039;in&#039;, invoice_obj.search(cursor, user,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(fargs[1], args[i][1], args[i][2])]))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 1<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isinstance(args[i][2], basestring):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res_ids = invoice_obj.name_search(cursor, user, args[i][2], &#91;],<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i][1])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i] = (args[i][0], &#039;in&#039;, [x[0] for x in res_ids])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 1<br />&nbsp; &nbsp; &nbsp; &nbsp; qu1, qu2 = &#91;], &#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; for x in args:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x[1] != &#039;in&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x[2] is False) and (x[1] == &#039;=&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id IS NULL)&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif (x[2] is False) and (x[1] == &#039;&lt;&gt;&#039; or x[1] == &#039;!=&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id IS NOT NULL)&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id %s %s)&#039; % (x[1], &#039;%s&#039;))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu2.append(x[2])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif x[1] == &#039;in&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(x[2]) &gt; 0:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id in (%s))&#039; % (&#039;,&#039;.join(&#91;&#039;%s&#039;] * len(x[2]))))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu2 += x[2]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039; (False)&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; if len(qu1):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1 = &#039; AND&#039; + &#039; AND&#039;.join(qu1)<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1 = &#039;&#039;<br /># 执行搜索语句,从两个 object的表里,搜索 这个ID,其实也就是上面一段产生的 根据输入的 args 产生的结果!<br />&nbsp; &nbsp; &nbsp; &nbsp; cursor.execute(&#039;SELECT l.id &#039; \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;FROM account_move_line l, account_invoice i &#039; \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;WHERE l.move_id = i.move_id &#039; + qu1, qu2)<br />&nbsp; &nbsp; &nbsp; &nbsp; res = cursor.fetchall()<br /># 这里也不太明白!貌似全部返回!<br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(res):&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [(&#039;id&#039;, &#039;=&#039;, &#039;0&#039;)]<br />&nbsp; &nbsp; &nbsp; &nbsp; return [(&#039;id&#039;, &#039;in&#039;, [x[0] for x in res])]
    




    另外,我要搜索的字段是 一个PO号,类似于 45000122458,虽然看上去是 数字,但是系统定义的类型是 char
    而输入搜索条件的时候,却是4500122这样的数字,恐怕要先把这个数字转换成 字符型!

    先请大家有空的时候,帮忙解释上面的那段语句吧!
    这里先谢谢了![/size][/size]



  • def _sappo_search(self, cursor, user, obj, name, args):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(args):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &#91;]<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; sappo_obj = self.pool.get(&#039;purchase.order&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; fp = file(&#039;ping.txt&#039;,&#039;a+&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; fp.write(str(args)+&#039;\n&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; fp.close<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; i = 0<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; while i &lt; len(args):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fargs = args[i][0].split(&#039;.&#039;, 1)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(fargs) &gt; 1:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i] = (fargs[0], &#039;in&#039;, sappo_obj.search(cursor, user,<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(fargs[1], args[i][1], args[i][2])]))<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 1<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isinstance(args[i][2], basestring):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res_ids = sappo_obj.name_search(cursor, user, args[i][2], &#91;],<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i][1])<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i] = (args[i][0], &#039;in&#039;, [x[0] for x in res_ids])<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 1<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; qu1, qu2 = &#91;], &#91;]<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; for x in args:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x[1] != &#039;in&#039;:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x[2] is False) and (x[1] == &#039;=&#039;):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id IS NULL)&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif (x[2] is False) and (x[1] == &#039;&lt;&gt;&#039; or x[1] == &#039;!=&#039;):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id IS NOT NULL)&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id %s %s)&#039; % (x[1], &#039;%s&#039;))<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu2.append(x[2])<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif x[1] == &#039;in&#039;:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(x[2]) &gt; 0:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039;(i.id in (%s))&#039; % (&#039;,&#039;.join(&#91;&#039;%s&#039;] * len(x[2]))))<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu2 += x[2]<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039; (False)&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if len(qu1):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1 = &#039; AND&#039; + &#039; AND&#039;.join(qu1)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1 = &#039;&#039;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; SQL = (&#039;SELECT l.id &#039; \<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;FROM stock_production_lot l, purchase_order i &#039; \<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;WHERE l.order_id = i.id &#039; + qu1, qu2)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; fp = file(&#039;pingsql.txt&#039;,&#039;a+&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; fp.write(str(SQL)+&#039;\n&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; fp.close<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; cursor.execute(&#039;SELECT l.id &#039; \<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;FROM stock_production_lot l, purchase_order i &#039; \<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;WHERE l.order_id = i.id &#039; + qu1, qu2)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; res = cursor.fetchall()<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(res):<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [(&#039;id&#039;, &#039;=&#039;, &#039;0&#039;)]<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; return [(&#039;id&#039;, &#039;in&#039;, [x[0] for x in res])]
    



    输出的 args如下


    [('sappo', 'ilike', '4500')]

    [('sappo', 'ilike', '4500')]

    [('sappo', 'ilike', '4500')]

    [('sappo', 'ilike', '4500')]

    而得到的SQL语句却没有任何参数得到!

    ('SELECT l.id FROM stock_production_lot l, purchase_order i WHERE l.order_id = i.id  AND (False)', [])

    按道理,'4500'类型应该是  string.
    却没有执行里面的循环!


  • 管理员

    不知道你这段代码来自哪里,但是可以肯定的是这段代码写得非常有问题。你如果能告诉我们这段代码在哪个模块里写在那个类里,对分析代码会有帮助。

    为了让你理解这段代码的意思,你可以试着假设args = [('module1.field1','like', 'something'), ('module2.field2','=', 1)] ,然后代入代码中
    fargs 就是 [module1, field1]或者 [module2, field2]
    再继续下去你应该就能理解这段代码了。

    但是,还是不建议你读这段代码,写得实在不咋的,会把你带坏的 😛



  • 这段源代码是 acount_move_line.py 第284行开始!
    功能是定义了一个fucntion字段,但是function字段搜索功能是要自己创建的!

    我自己也定义了一个function字段,也想创建这个搜索功能!
    args = [('module1.field1','like', 'something'), ('module2.field2','=', 1)] ,您说的这个我明白的!
    但是我遇到做的程序就一直出错!

    不是程序流程的错误!而是字段类型的问题!
    当我把搜索的,module2.field = 1,这里的feild 选择为 int 型的,功能没有问题,可以实现!
    但是如果 feild 为char型的,而不巧的是,我的char 确实一串 45007846这样的数字,就报错了! In types charcter varying and interger cannot be matched



  • 还是你的总体需求, 不被大家了解. 所以不好帮到你.

    1 你改了哪些东西?

    2 你在哪个对象上面添加了 function 字段?

    3 在这个对象的搜索里面, 你希望 从哪些数据中搜索到? 也就是你期望 的结果 是一个什么样的查询?



  • [quote author=mrshelly link=topic=2156.msg6780#msg6780 date=1284083072]
    还是你的总体需求, 不被大家了解. 所以不好帮到你.

    1 你改了哪些东西?

    2 你在哪个对象上面添加了 function 字段?

    3 在这个对象的搜索里面, 你希望 从哪些数据中搜索到? 也就是你期望 的结果 是一个什么样的查询?
    [/quote]

    shelly, 首先谢谢你给的帮助!
    第一个问题, 我的系统已经被改的面目全非, 所以呢,沟通起来有些麻烦!
    第二个问题,function字段,是我从 Purchase order里抽取了一个字段sappo到 stock_tracking.
    第三个问题,在stock_tracking tree上,能够按照sappo搜索 tree.
    其实就是stock_tracking有个function字段 SAPPO
    给他写个search, 可以搜索而已!
    经过反复测试 ,以及 shelly,黑JJ,校长的指导和帮助!
    得到以下的代码!
    虽然得到了结果,不过感觉写的不是很好!
    最重要的是,给我一楼的代码 误导了不少!一直在考虑那些个 frags啊什么的!
    其实中心就是, 根据args 搜索 stock_tracking 的ID, 返回。
    然后 ID就是  strock_tracking 里 order_id 和purchase_order.id ,purchase_order.sap_po ilike args.
    这里我抛弃了 Ilike,直接用了 ~*,
    功能是实现了!但是不是很好的一段代码!
    还请大家指正!


    def _sappo_search(self, cursor, user, obj, name, args):<br />&nbsp; &nbsp; &nbsp;  # fp = file(&#039;pingargs.txt&#039;,&#039;a+&#039;)<br />&nbsp; &nbsp; &nbsp;  # fp.write(str(args)+&#039;\n&#039;)<br />&nbsp; &nbsp; &nbsp;  # fp.close&nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(args):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &#91;]<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; qu1, qu2 = &#91;], &#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; for x in args:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x[1] == &#039;ilike&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(x[2]) &gt; 0:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039; i.sap_po ~*&#039; )<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu2 = x[2]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1.append(&#039; (False)&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; if len(qu1):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1 = &#039; AND&#039; + &#039; AND&#039;.join(qu1)<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qu1 = &#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; sql =&nbsp; &quot;&quot;&quot; SELECT l.id FROM stock_tracking l, purchase_order i WHERE l.order_id = i.id&nbsp; %s&#039;%s&#039;&quot;&quot;&quot; % (qu1, qu2,) <br />&nbsp; &nbsp; &nbsp;  # fp = file(&#039;pingsql.txt&#039;,&#039;a+&#039;)<br />&nbsp; &nbsp; &nbsp;  # fp.write(str(sql)+&#039;\n&#039;)<br />&nbsp; &nbsp; &nbsp;  # fp.close&nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; cursor.execute(sql)<br />&nbsp; &nbsp; &nbsp; &nbsp; res = cursor.fetchall()<br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(res):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [(&#039;id&#039;, &#039;=&#039;, &#039;0&#039;)]<br />&nbsp; &nbsp; &nbsp; &nbsp; return [(&#039;id&#039;, &#039;in&#039;, [x[0] for x in res])]
    


  • 感谢分享,请校长及shelly及时回复为盼。



  • 严重看不懂... 严重不关注...
    严重事不关已....