跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 新手求助
  4. 关于openerp中create的不解

关于openerp中create的不解

已定时 已固定 已锁定 已移动 Odoo 新手求助
16 帖子 5 发布者 10.4k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • L 离线
    L 离线
    l315276651
    写于 最后由 编辑
    #1

      我是一名开发人员,现在在学习开发openerp 现在遇到一个关于create的问题,有点不解,希望老鸟们指点指点。
    我想通过pymssql 将mssql中现有的数据 导入到openerp中备用 现在已经可以导入成功了,但却多了一条id为0的数据
    表结构很简单(id,name) form中 只有一个button  我很纳闷不知怎么回事。 现在将 id和name 添加到form中,
    输入值后 点击button,界面上的数据先写入,然后是MSSQL的数据,我现在只想写自己的数据 怎么做?
    以下是 xml代码

    <br /> &lt;!--定义表单视图--&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;record model=&quot;ir.ui.view&quot; id=&quot;view_mynorthwind_categories_form&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;mynorthwind.categories.form&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;type&quot;&gt;form&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;model&quot;&gt;mynorthwind.categories&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 	&lt;form&gt;<br />	&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;button name=&quot;categories_writer&quot; type=&quot;object&quot; string=&quot;导入MSSQL数据&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 	&lt;/form&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/record&gt;<br />&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;!--定义列秒视图--&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;record model=&quot;ir.ui.view&quot; id=&quot;view_mynorthwind_categories_tree&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;mynorthwind.categories.tree&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;type&quot;&gt;tree&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;model&quot;&gt;mynorthwind.categories&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 	&lt;tree&gt;<br />	&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 	&lt;field name=&quot;categoriesId&quot;/&gt;<br />	&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;field name=&quot;categoriesName&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 	&lt;/tree&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/record&gt;<br />
    


    以下是 py代码

    <br /># -*- coding: utf-8 -*-<br />from osv import osv<br />from osv import fields<br />import pymssql<br /><br /><br />class mynorthwind_categories(osv.osv):<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; _name=&#039;mynorthwind.categories&#039;<br />&nbsp; &nbsp; _description=&#039;分类&#039;<br />&nbsp; &nbsp; _columns={<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;categoriesId&#039;:fields.integer(&#039;ID&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;categoriesName&#039;:fields.char(&#039;Name&#039;,size=128)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; <br />&nbsp; &nbsp; def categories_writer(self, cr, uid, ids, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; print &quot;begin connect to the server RD5&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; conn=pymssql.connect(server=&quot;RD5-hgh&quot;,user=&quot;sa&quot;,password=&quot;permain&quot;,database=&quot;mynorthwind&quot;,charset=&quot;UTF-8&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; print &quot;connect success&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; cur = conn.cursor()<br />&nbsp; &nbsp; &nbsp; &nbsp; sql = &quot;SELECT top 10 CategoryID,CategoryName FROM Categories&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; cur.execute(sql)<br />&nbsp; &nbsp; &nbsp; &nbsp; resault=cur.fetchone()<br />&nbsp; &nbsp; &nbsp; &nbsp; i=0<br />&nbsp; &nbsp; &nbsp; &nbsp; while resault:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print &quot;categoriesId=%s , categoriesName=%s&quot;&nbsp; %&nbsp; (resault[0], resault[1])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowid= self.create(cr, uid,{ &#039;categoriesId&#039;:resault[0] ,&#039;categoriesName&#039;:resault[1] })<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resault = cur.fetchone() <br />&nbsp; &nbsp; &nbsp; &nbsp; conn.close() <br />mynorthwind_categories()<br />
    


    console 结果

    <br />pydev debugger: starting<br />begin connect to the server RD5<br />connect success<br />categoriesId=1 , categoriesName=手机<br />categoriesId=2 , categoriesName=MP3/4<br />categoriesId=3 , categoriesName=U盘<br />categoriesId=4 , categoriesName=存储卡<br />categoriesId=5 , categoriesName=项链<br />categoriesId=6 , categoriesName=手链<br />categoriesId=7 , categoriesName=耳环<br />categoriesId=8 , categoriesName=发饰<br />categoriesId=9 , categoriesName=Test statistics<br />
    


    SQL 查询结果
    CategoryID  CategoryName
    ----------- --------------------
    1          手机
    2          MP3/4
    3          U盘
    4          存储卡
    5          项链
    6          手链
    7          耳环
    8          发饰
    9          Test statistics

    OE里面的数据(图片不知道怎么上传,手动打上去的)
                    ID          NAME
            0
    1 手机
    2 MP3/4
    3 U盘
    4 存储卡
    5 项链
    6 手链
    7 耳环
    8 发饰
    9 Test statistics
    1 手机
    2 MP3/4
    3 U盘
    4 存储卡
    5 项链
    6 手链
    7 耳环
    8 发饰
    9 Test statistics
    这是点击2次后的结果
    很明显 当表里面的数据为空的时候 会默认的插入一条 ID=0 的数据
    我想去掉这条数据 怎么操作?
    这样描述 应该很清楚吧

    1 条回复 最后回复
    0
    • J 离线
      J 离线
      Joshua 管理员
      写于 最后由 编辑
      #2

      没看明白,你的从mysql读出来的resault是10条,执行create后,openerp有11条(多了id=0的)?

      【上海先安科技】(joshua AT openerp.cn),欢迎关注公众号:openerp_cn

      1 条回复 最后回复
      0
      • L 离线
        L 离线
        l315276651
        写于 最后由 编辑
        #3

        [quote author=Joshua link=topic=6716.msg15996#msg15996 date=1369818824]
        没看明白,你的从mysql读出来的resault是10条,执行create后,openerp有11条(多了id=0的)?
        [/quote]
        是的 很奇怪 不知道为什么会这样

        1 条回复 最后回复
        0
        • L 离线
          L 离线
          liwei78
          写于 最后由 编辑
          #4

          我觉得你的意思是,要保持导入之前的记录和OE里的记录,ID一致,是么?

          1 条回复 最后回复
          0
          • M 离线
            M 离线
            mrshelly
            写于 最后由 编辑
            #5

            你的循环没有用对....

            1 条回复 最后回复
            0
            • D 离线
              D 离线
              digitalsatori 管理员
              写于 最后由 编辑
              #6

              多出来的记录值是什么?是重复值还是空值。为什么要用super? 类名也用的奇怪

              【上海先安科技】(tony AT openerp.cn)

              1 条回复 最后回复
              0
              • L 离线
                L 离线
                l315276651
                写于 最后由 编辑
                #7

                [quote author=digitalsatori link=topic=6716.msg16003#msg16003 date=1369839636]
                多出来的记录值是什么?是重复值还是空值。为什么要用super? 类名也用的奇怪
                [/quote]
                多出来的记录是 id:0 name 空, 我读到的数据是 id:1,name:xxxx 这样的数据,id从1开始到9
                用super 是因为在点击按钮的时候 会自动执行create方法一次,想重写
                类名奇怪? 自己测试用的 不知道openerp对名称有什么特别的要求?

                1 条回复 最后回复
                0
                • L 离线
                  L 离线
                  l315276651
                  写于 最后由 编辑
                  #8

                  [quote author=Li.Wei link=topic=6716.msg15999#msg15999 date=1369828652]
                  我觉得你的意思是,要保持导入之前的记录和OE里的记录,ID一致,是么?
                  [/quote]
                  是的 我读出来的数据是 1到9 但写入后 却多了一个id=0 name=空的记录

                  1 条回复 最后回复
                  0
                  • L 离线
                    L 离线
                    l315276651
                    写于 最后由 编辑
                    #9

                    [quote author=mrshelly link=topic=6716.msg16000#msg16000 date=1369834010]
                    你的循环没有用对....
                    [/quote]
                    没用对?但我的数据已经全部写入oe了啊

                    1 条回复 最后回复
                    0
                    • M 离线
                      M 离线
                      mrshelly
                      写于 最后由 编辑
                      #10

                      [quote author=l315276651 link=topic=6716.msg16007#msg16007 date=1369880356]
                      [quote author=mrshelly link=topic=6716.msg16000#msg16000 date=1369834010]
                      你的循环没有用对....
                      [/quote]
                      没用对?但我的数据已经全部写入oe了啊
                      [/quote]

                      数据已写入, 也不表明你的循环就正确啊.

                      如果你的循环多运行了一次. 你想想后果是什么样子的?

                      五条记录, 你想象中 应该是循环五次, 每次插入一条记录,
                      但是,如果你的循环用错了. 你循环里的代码运行了六次. 那结果会如何呢? 当然, 这只是假设. 😄

                      1 条回复 最后回复
                      0
                      • L 离线
                        L 离线
                        l315276651
                        写于 最后由 编辑
                        #11

                        [quote author=mrshelly link=topic=6716.msg16016#msg16016 date=1369955570]
                        [quote author=l315276651 link=topic=6716.msg16007#msg16007 date=1369880356]
                        [quote author=mrshelly link=topic=6716.msg16000#msg16000 date=1369834010]
                        你的循环没有用对....
                        [/quote]
                        没用对?但我的数据已经全部写入oe了啊
                        [/quote]

                        数据已写入, 也不表明你的循环就正确啊.

                        如果你的循环多运行了一次. 你想想后果是什么样子的?

                        五条记录, 你想象中 应该是循环五次, 每次插入一条记录,
                        但是,如果你的循环用错了. 你循环里的代码运行了六次. 那结果会如何呢? 当然, 这只是假设. 😄
                        [/quote]
                        可以确认的是 循环没有问题 每写一次数据我都print 出来写入的数据。现在的问题点是 在我开始循环写入数据之前
                        OE就自动的帮我写入了一条数据,这条数据就是在tree页面 点击create后自动产生的,点击button后 oe会先写入这条
                        数据 然后才执行我的function ,我猜测 这是oe的一种机制。不知道有什么办法 可以屏蔽系统自动创建的数据

                        1 条回复 最后回复
                        0
                        • M 离线
                          M 离线
                          mrshelly
                          写于 最后由 编辑
                          #12

                          嗯. 问题就在这里.

                          你不应该到本身这个对象的 form view

                          而应该是一个 wizard (osv.osv_memory || osv.Model) 内存对象...

                          1 条回复 最后回复
                          0
                          • L 离线
                            L 离线
                            l315276651
                            写于 最后由 编辑
                            #13

                            [quote author=mrshelly link=topic=6716.msg16026#msg16026 date=1369966522]
                            嗯. 问题就在这里.

                            你不应该到本身这个对象的 form view

                            而应该是一个 wizard (osv.osv_memory || osv.Model) 内存对象...
                            [/quote]
                            有点不明白  不知道 源码中 有相应的demo吗?

                            1 条回复 最后回复
                            0
                            • J 离线
                              J 离线
                              Joshua 管理员
                              写于 最后由 编辑
                              #14

                              首先感谢@mrshelly大大的回复。
                              @l315276651 的问题主要出在当你打开一个表单视图的时候,点击任何按钮,服务器首先第一步是把你当前的表单视图保存(估计你的表单里面没有必填字段,所以会保存 名字:空 ,xx字段:空),id=0就这样出来了。如果你是打开一条原有的数据的表单视图再点击你的按钮,估计就不会再生成这条id=0。
                              解决办法:

                              1.用wizard(osv.osv_memory || osv.TransientModel) 估计mrshelly回复里面有个笔误。(具体代码可以看其他模块的wizard文件夹),openerp里面的动作(按钮,菜单)一般都要绑定一个model,而这里如果用了osv.TransientModel,首先他会弹出你定义的TransientModel的视图,点击按钮后也会保存一个TransientModel的记录,但是这个记录系统会隔一段时间自动清空。

                              2.你可以使用server action,action type 选择 python code(里面可以写你的自己的方法), 然后把server action可以绑定到菜单上,这个server action虽然也要绑定一个model,不过并不需要开启该model的任何表单。

                              【上海先安科技】(joshua AT openerp.cn),欢迎关注公众号:openerp_cn

                              1 条回复 最后回复
                              0
                              • L 离线
                                L 离线
                                l315276651
                                写于 最后由 编辑
                                #15

                                感谢mrshelly和Joshua 2位大大的回复
                                让我进一步的了解了OE
                                继续学习OE 支持OE ;D

                                1 条回复 最后回复
                                0

                                • 登录

                                • 没有帐号? 注册

                                • 登录或注册以进行搜索。
                                • 第一个帖子
                                  最后一个帖子
                                0
                                • 版块
                                • 标签
                                • 热门
                                • 用户
                                • 群组