如何从odoo 9中的数据库中获取数据

时间:2016-06-14 07:17:25

标签: xml python-2.7 openerp odoo-9

我有一段用于获取数据的代码,但它不起作用

py文件

 class group_js(osv.osv):
    _name = "group_js"
    _description = "Group JS"

    _columns={
       'id': fields.integer('ID', readonly=True),
       'name': fields.char('Purpose', required=True, select=1),
       'js' : fields.text('Javascript',required = True , index=True,store=True),
       'active' : fields.boolean('Active'),
       'group_id': fields.many2many('res.groups', 'js_group_rel', 'js_group_id', 'group_id', string='Groups'),
    }
   _sql_constraints = [
    ('name_uniq', 'unique (name)', 'The name of the Javascript available , You must change your javascript name or check the javascript code may available !!!')
   ]

   def get_record(self,cr,uid,context=None):
     get_obj=self.pool.get('group_js')
     user_ids = get_obj.browse(cr, uid,uid,[])
     return user_ids.to_JSON()

  def to_JSON(self,cr,context=None):
     return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)

1 个答案:

答案 0 :(得分:0)

  

浏览光标,用户, ID ,上下文

对于ID,您首先使用空白域 [] 在模型中搜索

尝试这种方式:

   def get_record(self,cursor,user,context=None):
     get_obj=self.pool.get('group_js')
     domain=[]
     ids = get_obj.search(cursor, user,domain)
     user_ids = get_obj.browse(cursor, user,ids,context)
     return user_ids.to_JSON()

或者这样

   def get_record(self,cursor,user,context=None):
     get_obj=self.pool.get('group_js')
     domain=[]
     ids = get_obj.search(cursor, user,domain)
     user_ids = get_obj.read(cursor, user,ids,[],context)
     return user_ids

已编辑:关于如何获取群组名称以及ID

由于group_idmany2many字段,因此在这种情况下,您将拥有browse以下记录并更新组group_id

group_obj = self.pool.get('res.groups')
def get_id_name(group_id):
    res =group_obj.browse(cursor, user,group_id)
    return res.id,res.name
user_ids[0]['group_id']=map(lambda group_id:get_id_name(group_id),user_ids[0]['group_id'])

现在该方法如下:

  def get_record(self,cursor,user,context=None):
    get_obj=self.pool.get('group_js')
    domain=[]
    ids = get_obj.search(cursor, user,domain)
    user_ids = get_obj.read(cursor, user,ids,[],context)
    group_obj = self.pool.get('res.groups')
    def get_id_name(group_id):
        res =group_obj.browse(cursor, user,group_id)
        return res.id,res.name
    user_ids[0]['group_id']=map(lambda group_id:get_id_name(group_id),user_ids[0]['group_id'])
    return user_ids