2015-12-02 41 views
0

我有一个向导写入数据库,但没有读取写入数据库的值并显示在视图中。 我使用get_opinion阅读的意见和get_notes阅读笔记,但是当我点击上面的按钮,他们只是没有错误,我要去哪里错了Odoo向导写入模型,但从模型获取值并不会填充向导字段

Q2,在这段代码是部分消失代码下面为什么我越来越model_name没有

if context is None: context = {} 
model_name=context.get('active_model') 
print model_name #gives NONE why ? 

问题3我如何列出上下文中的所有领域?

模块代码是如下

class opinion(models.TransientModel): 
_name='opinion' 
opinion_emission = fields.Text(string='OPINION EMISSION') 
notes = fields.Text(string='Additional Notes') 


defaults={ 
    'opinion_emission': lambda self : self.get_opinion(self), 
    'notes': lambda self : self.get_notes(self), 

} 
def save_it(self, cr, uid,ids , context=None): 
    # context = dict(self._context or {}) 
    if context is None: context = {} 

    active_id = context.get('active_id',False) 
    print 'active_id' , active_id 
    if active_id : 
     # op = self.env['opinion'].browse(active_id) 
     info = self.browse(cr,uid,ids) 
     self.pool.get('opinion').write(cr,uid,context['active_id'],{'opinion_emission': info[0].opinion_emission,'notes': info[0].notes}) 
    return { 
     'type': 'ir.actions.act_window_close', 
    } 


#functions that get the info stored in db 
@api.one 
def get_opinion(self): 
    ids=self._ids 
    cr = self._cr 
    uid = self._uid 
    context = self._context 
    if context is None: context = {} 
    active_id = context.get('active_id',False) 
    print 'active_id' , active_id 
    if active_id : 

     return self.env['opinion'].browse(cr, uid, context['active_id'], context).opinion_emission 


@api.one 
def get_notes(self,records=None): 
    ids=self._ids 
    cr = self._cr 
    uid = self._uid 
    context = self._context 
    if context is None: context = {} 
    model_name=context.get('active_model') 
    print model_name #gives NONE why ? 
    active_id = context.get('active_id',False) 
    print 'active_id' , active_id 
    if active_id : 
     print 'output',self.env['opinion'].browse(cr, uid, context['active_id'], context)[0].notes 
     return self.env['opinion'].browse(cr, uid, context['active_id'], context)[0].notes 

XML视图代码是:

<openerp> 
<data> 
    <record id="view_opinion_wizard" model="ir.ui.view"> 
     <field name="name">opinion_wizard.form</field> 
     <field name="model">opinion</field> 
     <field name="type">form</field> 
     <field name="arch" type="xml"> 
     <!-- create a normal form view, with the fields you've created on your python file --> 
      <form string="OPINION" version="8.0"> 
             <button icon="gtk-ok" name="get_notes" string='SET VAL' type="object" /> 

       <group > 
        <separator string="Please insert OPINION" colspan="2"/> 
        <field name="opinion_emission" string="OPINION EMISSION "/> 
              <field name="notes" string="NOTES"/> 

        <newline/> 
       </group> 
       <div style="text-align:right"> 
        <button icon="gtk-cancel" special="cancel" string="Cancel"/> 
              <button icon="gtk-ok" name="save_it" string="SAVE INFO" type="object" /> 
              <button icon="gtk-ok" name="get_notes" string="GET NOTES" type="object" /> 

        <button icon="gtk-ok" name="get_opinion" string="GET OPINION" type="object" /> 

       </div> 

      </form> 
     </field> 
    </record> 
    <!-- your action window refers to the view_id you've just created --> 
    <record id="action_opinion" model="ir.actions.act_window"> 
     <field name="name">OPINION</field> 
     <field name="type">ir.actions.act_window</field> 
     <field name="res_model">opinion</field> 
     <field name="view_type">form</field> 
     <field name="view_mode">form</field> 
     <field name="view_id" ref="view_opinion_wizard"/> 
     <field name="target">new</field> 
    </record> 
      <menuitem name="OPINION" id="menu_opinion" action="action_opinion" parent="timeorder_ratecard_rnd_menu"/> 

</data> 

回答

1

这是新的API代码尝试此代码:

class opinion(models.TransientModel): 
    _name = 'opinion' 
    opinion_emission = fields.Text(default=lambda self: self.get_opinion(self), string='OPINION EMISSION') 
    notes = fields.Text(default=lambda self: self.get_notes(self), string='Additional Notes') 

    @api.multi 
    def save_it(self): 
     active_id = self.env.context.get('active_id', False) 
     print 'active_id', active_id 
     if active_id: 
      op = self.env['opinion'].browse(active_id) 
      op.write({'opinion_emission': self.opinion_emission, 'notes': self.notes}) 
     return {'type': 'ir.actions.act_window_close', } 
    #functions that get the info stored in db 

    @api.one 
    def get_opinion(self): 
     active_id = self.env.context.get('active_id', False) 
     print 'active_id', active_id 
     if active_id: 
      return self.env['opinion'].browse(active_id).opinion_emission 

    @api.one 
    def get_notes(self, records=None): 
     model_name = self.env.context.get('active_model') 
     print model_name  # gives NONE why ? 
     active_id = self.env.context.get('active_id', False) 
     print 'active_id', active_id 
     if active_id: 
      print 'output', self.env['opinion'].browse(active_id).notes 
      return self.env['opinion'].browse(active_id).notes