Odoo Wizard写入模型,但从模型获取值并不会填充向导字段

时间:2015-12-02 15:20:49

标签: openerp odoo-8 openerp-8

我有一个向下写入数据库的向导,但没有读取写入数据库的值并显示在视图中。 我使用get_opinion阅读意见和get_notes阅读笔记,但是当我点击按钮时它们就会消失而没有错误,我哪里出错了

Q2,在此代码中,这是下面代码的一部分,为什么我得到model_name none

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

Q3如何在上下文中列出所有字段?

模块代码如下

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 个答案:

答案 0 :(得分: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