Thursday, 4 August 2016

Show or Call Wizard from Python Code Odoo


Hello world. You know this is for me to refresh my brain at work. Today i just got task, very simple but sometimes we need to create with similar case that i have. So i just want to show or calling wizard, but its not from xml code but on the python code. On the previous post here, i just show wizard on the xml code, but now i try to show you different way its from python code. Lets do it.

So as you know wizard is model, but they have type TransientModel, it means they just save in temporary memory not to primary memory (database).  Ya i just want you to know it, its first principle that i don't know before my friend told me. Lol.

This is my function to call wizard :

@api.multi
def call_wizard(self):
    print "call wizard fucntion"
    wizard_form = self.env.ref('custom_wizard.cutom_wizard_view', False)
    view_id = self.env['dedy.yuristiawan.wizard']
    vals = {
                'name'   : 'this is for set name',
            }
    new = view_id.create(vals)
    return {
                'name'      : _('Hi I am wizard, I am from python code'),
                'type'      : 'ir.actions.act_window',
                'res_model' : 'dedy.yuristiawan.wizard',
                'res_id'    : new.id,
                'view_id'   : wizard_form.id,
                'view_type' : 'form',
                'view_mode' : 'form',
                'target'    : 'new'
            }

As you can see, on the function i can get conclusion like this :

1. Declaration your wizard, like where is the wizard position. like this one :

wizard_form = self.env.ref('custom_wizard.cutom_wizard_view', False)

I just declaration my module custom_wizard and the template id cutom_wizard_view and then i put to variable wizard_form .   

2. Create record to get id wizard. like this one : 

        view_id = self.env['dedy.yuristiawan.wizard']
    vals = {
                'name'   : 'this is for set name',
            }
    new = view_id.create(vals)

in this code, i just call model and then i do create one record, set name (cause in my wizard model i just have one field is name ). And then i put to variable new . This variable is for get id, i can call with new.id . 

3. Return Your function, to call wizard. Use this code :

return {
                'name'      : _('Hi I am wizard, I am from python code'),
                'type'      : 'ir.actions.act_window',
                'res_model' : 'dedy.yuristiawan.wizard',
                'res_id'    : new.id,
                'view_id'   : wizard_form.id,
                'view_type' : 'form',
                'view_mode' : 'form',
                'target'    : 'new'
            }

name is for title wizard, 
type for call action window, 
res_model is for set our wizard model, 
res_id is id record that we create before. 
view_id is view that we create before, 
view_type and view_mode is to set from for wizard, 
and the last is target new for create new action. 

Just it, you can read first, if you still confuse about my code you can fill comment bellow. I hope this code can help you little bit.


Share:

Monday, 1 August 2016

How to Create Wizard on Odoo ?



https://dedyyuristiawan.blogspot.com/


Hello world. As i promise on this post How to Call Wizard from XML Button Odoo, i will share about wizard on Odoo. As you can see on previous post i just call wizard on the xml file. Now, i will share how to create wizard on Odoo. You know, wizard on Odoo is like pop up when in website, so in wizard data will save in memory, not in database Odoo. Usually we show wizard is for input additional data. So lets do it.

On this post, i just share wizard, if you want call wizard, you can read on this post How to Call Wizard From XML Button Odoo. So you can on my wizard.xml file bellow.

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="dedy_yuristiawan_wiz" model="ir.ui.view">
            <field name="name">dedy.yuristiawan.wizard.name</field>
            <field name="model">dedy.yuristiawan.wizard</field>
            <field name="arch" type="xml">
                <form string="Hi i am wizard">
                    <group>
                        <field name="name"/>
                    </group>
                    <footer>
                        <button name="create_request" 
                            string="Finished" type="object" 
                            class="btn-primary"/>
                        <button string="Cancel" 
                            class="btn-default" 
                            special="cancel" />
                    </footer>
                </form>
            </field>
        </record>

        <record id="action_view_wizard" model="ir.actions.act_window">
            <field name="name">Hi i am a wizard</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">dedy.yuristiawan.wizard</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="dedy_yuristiawan_wiz"/>
            <field name="target">new</field>
        </record>
    </data>
</openerp>

Ass you can see, just 2 record on my xml file. On the bottom record i have name, type, model, view type, view mode, view_id, and target. The important is :

1. Type ir.actions.act_window it mean will show window like from view usually.

2. Res_model dedy.yuristiawan.wizard it's my model.

3. Target new it will show as new form, you can set current, it mean will show your current form.

4. View Id dedy_yuristiawan_wiz it will call top record as you can see. You know Odoo its run from Top to Bottom, That's why i put dedy_yuristiawan_wiz on the top (it mean, i make declaration first before i call it). 

5. On the Top record at  it look like similar with bottom record, i just add field, that i create on python file, and two button, button Cancel will dismiss this wizard and button Finish will call function create_request on my python file.

And this one, i call it python file.

from openerp import models, fields, api, _
from openerp.exceptions import UserError, ValidationError, Warning


class DedyYuristiawan(models.TransientModel):
    _name = "dedy.yuristiawan.wizard"
    
    name = fields.Char('Name', required=True)
    
    @api.multi
    def create_request(self):
        print "You click finish"

        return True

As you can see, the class have name models.TransientModel it mean wizard. And then i create field name that i call in  my xml file <field name="name"/> . Last i create function def create_request(self). That's i told you before, if want to call this function i create button Finish with this syntax <button name="create_request" string="Finished" type="object" button with name and type object.

That's it. So if want make wizard you can read first, you can create code python first and then xml code. Aha i forget something, Usually these file (python and xml) i put on folder Wizard, and the i declare on the __init__.py with include python file and then on __openerp__.py with this 'wizard/split_wo_wizard.xml' put on the top.

It real that's it. Aha. Just read first, if you still confuse about my code, you can write comment bellow. Thanks.
Share:

Sunday, 17 July 2016

How to Call Wizard from XML Button Odoo

Hello world. Wizard is important if you want to develop odoo, wizard is something like pop up will show when you have action. With wizard you can make form or just information that you want to show. Okay, at first i wanna show you how to call wizard form xml button. And the next i will show you how to make wizard in odoo. Lets make it happen.

This is my code for call wizard from button.

<button
       type="action"
       name="%(split_wo.action_view_wizard)d"
       string="Finished"
       class="oe_highlight"
       help="Finish Order"/>

in this code the important is name, as you can se split_wo.action_view_wizard, so split_wo is module where is the wizard, and action_view_wizard is name of the wizard. Simple right? that's it. So if you have question, you can write comment bellow, don't worry i will help you if i can. I hope this is can help you for little bit. lol
Share:


<br>


Advice for contacting me at dedyy54@gmail.com