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:

Friday, 29 July 2016

How to Create Controller or Link Odoo Website ?



https://dedyyuristiawan.blogspot.com


Hello world. Today I got happy felling but i don't where it come from? Lol just for laugh. So today i am gonna share about controller in Odoo. Actually for the first time i hear that, i don't know what should i do? okay in this time, i will try to make definition from myself, you can make your definition too on the comment bellow, we make definition together.

Controller is class on Odoo that can use for us when we want to make link on website, and then show anything that what we want to show on your website. I hope like that, so i am not give you wrong way. So how to create controller on Odoo? Lets see.

This is my class controller, my function on cons.py file :

import logging
from openerp import SUPERUSER_ID
from openerp import http
from openerp.http import request
from openerp import models, api

class website_qty(http.Controller):
 
    @http.route(['/page/dedy'], type='http', auth="public", website="True")
    def index(self, **post):
        wcl = []
        dedy = request.env['dedy.yuristiawan']
        dy = dedy.search([], state='draft')
        return request.render('dedy_controller.index_assets_frontend_view', {              
            'dedy': dy,
        })

As you can see, i can make conclution for this code :

1. Name of the class, you can create name anything what you want, but in this class we need to declaration first. For declaration you can use this code.

http.Controller

2. Name of the function, as you can see you can create name anything what you want, but in the above of name function you need to declaration your url, some thing like this.

@http.route(['/page/dedy'], type='http', auth="public", website="True")

3. Return , actually for return its not really need similar with on python, but in controller you need to render for show data to xml view. something like this.

return request.render('dedy_controller.index_assets_frontend_view', {              
            'dedy': dy,
        })

dedy_controller is my module, index_assets_frontend_view is my id in xml file, and then the important id dedy is variable that we want to show in xml file or on website.

4. Inside of the function you can do anything what you want. like in my code, i just show data from my model dedy.yuristiawan, and then i set in return on variable dedy.

5. Just it.

And this one its my cons.xml file :

<template id="index_assets_frontend_view">
    <title>Test Input</title>
    <t t-call="website.layout">
        <t t-set="title">Dedy Yuristiawan</t>
            <div class="oe_structure">
                <div class="container">
                    <t t-foreach="dedy" t-as="value">
                        <td class="brctbl-col4 js_loc">
                            <p>
                                <t t-esc="value.name"/>
                            </p>
                        </td>
                    </t>
                </div>
            </div>
    </t>
</template>

As you can see, i can make conclution for this code :

1. Template id, its really important for controller, cause usually function on controller bring value to xml with calling template name. See my conclution cons.py file number three.

<template id="index_assets_frontend_view">

2. As you can see i do looping with variable dedy, this variable is from function controller that we set on return. You can see, my conclution cons.py file number 3.

<t t-foreach="dedy" t-as="value">

t-as, it means you can you put value dedy to variable value.

3. And last you had variable, you can do anything, show anything to website, with qweb sintax Odoo. like this looping, show field.

looping :

<t t-foreach="dedy" t-as="value">

show value :

<t t-esc="value.name"/>

4. Actually you can make link on xml code, you can use action on form or just (a href) and link will be the call controller function python.

<a t-att-href="'/page/dedy_next_view/" name="dedy_next_id" class="btn btn-default">Go to next link</a>

action="/page/test_input_view_form" method="post"

5. Last one, if you want to make post method, you need to add input like this one.

<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>

This is for security to protect our website, that we need get token from Odoo. This is rule for the Odoo 9.

That's it, you can write comment bellow if you still confuse about my code. i hope this can help you little bit. Ahha

Share:

How to Inherit Function Odoo ?



https://dedyyuristiawan.blogspot.co.id


Hello wold. Today i got inspiration from my comment's. He just started develop Odoo. You know, actually i am so appreciate that, maybe we feel difficult to learn something new, and we don't have person to help us, just world wide web. I know, because for first time I learn Odoo, i feel so disappointed for myself, I just think 'i can't do it'. But i keep learn Odoo, cause i am still work with company use Odoo aha. But huyaa for time by time i can learn the process Odoo, new syntax from python, qweb, or maybe js, css, etc cause my background its not from website. And you know, now i am realise that how's can develop Odoo is easy, i am so realise that. Just little bit share my misery. Lol

So just go ahead right? today i am gonna share about how to inherit function Odoo, for example in this post i will inherit function create. Just for note, function create is automatically create when we develop view form or tree. Aha you know function create in base Odoo, it always have the name 'create'. And usually have one parameter is self or two parameter is self, vals (variable that we have). For example in Odoo base, we have create like this.

@api.model
def create(self, vals):
    .
    .
    .
    .
    return A

As you can see create have two variable, self and vals. And now i want to inherit that. see my code.

class AddFieldProduct (models.Model):
    _inherit = "product.template"    
 
    info = fields.Char('Info') 

    @api.model
    def create(self, vals):
        if self.info :
            self.info = 'is a field info'
        return super(AddFieldProduct, self).create(vals)

as you can see, the requirement is :

1. We need to create file something like me, i create file inherit_function.py and then we need to create class like 'class inherit_function (models.Model)' and type class 'model'.

2. Declaration class with _inherit = "product.template", it mean we want to inherit model product.product.

3. Write name and parameter, similar with function, see def create(self, vals) and add @api.model above the function.

4. Add what you what you want need. Like this one i just want to set field info is 'is a field info'. if self.info : self.info = 0

5. And last add return with super like this one, return super(inherit_function, self).create(vals). inherit_function is a my class, see on the top.

7. When we inherit create like that, we put there condition on the function, field info will save 'is a field info' in database. trust me.

6. That's it, easy right? 

So the conclusion is Semangat!! like my boss always say to me, when i got problem with my code. Lol


Share:

Monday, 25 July 2016

How to make sequence name Odoo



http://dedyyuristiawan.blogspot.com


Hello world. Today i am gonna share about little trick in Odoo, this is a how to make field name sequence. Actually its basic from Odoo, but i just know when i got task like that. Lol. So just follow my step by step.

1. Declaration code for sequence

For declaration sequence i create xml file like this. the code is test.dedy.yuristiawan, prefix DY, padding is 5, and it would be like this 'DY00001'. Prefix is for first char (DY) and padding is for long number (00001). You can call this sequence with test.dedy.yuristiawan. See this code

  <?xml version="1.0" encoding="utf-8"?>  
  <openerp>  
    <data noupdate="1">  
      <record id="seq_test_dedy_yuristiawan" model="ir.sequence">  
        <field name="name">Test Dedy Yuristiawan</field>  
        <field name="code">test.dedy.yuristiawan</field>  
        <field name="prefix">DY</field>  
        <field name="padding">5</field>  
        <field eval="1" name="number_increment"/>  
      </record>  
    </data>  
  </openerp>  

and then i put this code in test_dedy_yuristiawan.xml

2. Override Function set Field Name, before Create.

Yap, now i override create function like this. This code i set field name readonly=True (anybody can't change) and call  self.env['ir.sequence'].next_by_code('test.input.machine') that i create in xml file. If you don't know about override function i will create specially post for How to Override Function in Odoo in the next post.

name = fields.Char('Code Dedy', readonly=True)

@api.model
def create(self, vals):
    print "+++++++++++++++++++++++++++++++++++++++++++++++++"
    print "prepare inherit create function"
    print "change the name TI with sequence"
    vals['name'] = self.env['ir.sequence'].next_by_code('test.input.machine')
    print "Inherit complete"
    print "+++++++++++++++++++++++++++++++++++++++++++++++++"
    return super(test_input_machine, self).create(vals)


3. Declaration xml file into __openerp__.py

Last step don't forget to declaration xml file to __openerp__.py

'data'          : [
                         'data/test_dedy_yuristiawan.xml',

That it, i hope this is can help you little bit, so if you still confuse about my code, you fill comment bellow and i will try to help you later. Thanks.



Share:

Saturday, 23 July 2016

Installation Odoo on Mac OS X Error Could not execute command 'lessc'



https://dedyyuristiawan.blogspot.co.id


Hello world. As you know before i create this post, i did post about installation on mac with bitnami version and home-brew. There so many difference between bitnami and home-brew, you can check on my previous post. So when i install Odoo with home-brew, i got a one problem. You can see in this post.

The problem is my library python not complete to run Odoo, as you can see the problem is "Could not execute command 'lessc' ", i just need to install less in my python library. so i going to find solution on google. And finally i got solution to make Odoo on my mac with this command ( type without  $ ).

$ brew update
$ brew uninstall node
$ brew install node
$ sudo brew postinstall node

so first step i just need to update my brew, and uninstall node that i ever install, and then install node again, i make force to post install node. I hope this post can help you and see you.
Share:

How to Install Odoo On Mac with Packages Homebrew



https://dedyyuristiawan.blogspot.co.id


Hello world. How are you today ? Are want to install odoo 9 on mac with packages home-brew,  now you can make it happen in your mac. Just do my step by step and i will give you perfect way to get odoo in your mac. In this post i will show you how to install odoo on mac with packages homebrew. Iam so really interested talk about home-brew cause with this packages we can do anything like we use linux on your mac. 

As you know home-brew is a "the missing packages for os x". With Homebrew we can install packages in the console as we can in the most Linux distributions. So first step you need install with this command in your console (terminal) :

1. Install Homebrew  :


  ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Install Python with Brew

After this we can install Python. If you already did this with a downloaded installer you can skip installing Python with Homebrew but I cannot guarantee for that. If you have problems then uninstall Python and try the package from Homebrew. To install run and follow the instructions:


  brew update
  brew install python

3. Install PostgreSQL

Now we have installed Python. Next we need the PostgreSQL server. Here we can also use Homebrew:

  brew install postgresql

remember if you have already odoo on your mac, using this command first before you run odoo. To start the PostgreSQL right now and automatically at system startup we run:

4. Install some Dependencies for the Pillow module

  brew install freetype jpeg libpng libtiff webp xz

5. Install Dependencies Python Module for Odoo

Next we can decide how to install the Python dependencies for odoo. One way is to install these globally. If you developing on other Python applications you sometimes need the same Python packages but in different versions. Or you want to let the system clear and remove these easy when you do not need them anymore. To get these advantages we can create a virtual environment for Python. This virtual environment copies Python and some other files (like pip) to a specified directory (e.g. in your home path) and provides a script which rewrites the path to use the Python from this directory.

To install the Python dependencies we use pip which is package manager for Python (like Homebrew but for Python modules) and is already installed with Python. For creating the virtual environment we have to install virtualenv. Homebrew does not contain virtualenv so we install it with pip (globally):

  pip install virtualenv

Now we can create the virtual environment. We use for this the directory odoo-env in our home directory:

  virtualenv ~/odoo-env

With this command all necessary files were copied to this directory. Next we enter the created virtual environment:

  . ~/odoo-env/bin/activate

Okay, all looking the same as before? :) We can look if we are in this virtual environment:

  which python
  /Users/youruser/odoo-env/bin/python

As you can see the Python binary from this directory is used. If you want to leave this environment you can type deactivate or close the terminal. Be aware that if you open a new tab or reopen the terminal you are not in the environment. Here you have to run the activate script as before.

Now we can start installing all dependencies for odoo. Download odoo if you did not have (e.g. clone the git repository) . In the newer odoo versions there is a requirements.txt file which contains all dependencies. To install these you have to change to the odoo directory (with cd) and run (you have to be in your virtual environment):

  pip install -r requirements.txt

if you get some text like this, you should be happy. Lol

  Successfully installed [...]
  Cleaning up...

You have installed all dependencies. Now you can start odoo with this command :

  python ./odoo.py

and will show like this.

2014-12-16 09:52:28,172 2974 INFO ? openerp: OpenERP version 8.0
2014-12-16 09:52:28,172 2974 INFO ? openerp: addons paths: ['/Users/youruser/Library/Application Support/Odoo/addons/8.0', u'/Users/youruser/odoo/openerp-v8/openerp/addons', u'/Users/youruser/odoo/openerp-v8/addons']
2014-12-16 09:52:28,172 2974 INFO ? openerp: database hostname: localhost
2014-12-16 09:52:28,172 2974 INFO ? openerp: database port: 5432
2014-12-16 09:52:28,172 2974 INFO ? openerp: database user: youruser
2014-12-16 09:52:28,328 2974 INFO ? openerp.service.server: HTTP service (werkzeug) running on 0.0.0.0:8069
2014-12-16 09:53:32,095 2974 INFO ? openerp.addons.bus.bus: Bus.loop listen imbus on db postgres
…

Then you can open your browser and open http://localhost:8069 .

Here you can create a new database with or without demo data and playing/developing with odoo. If you done you can stop with CTRL+C in your console.

Now you can create database, user name and password for do development in first user interface. After you have done with first set up, you will meet the backend UI Odoo 9.


https://dedyyuristiawan.blogspot.co.id

You can install all apps that you want or you want to develop custom module for Odoo 9. But remember as you can see, we run with python so you need to start and restart when you want run or stop Odoo.

For start service you can use this command :

  virtualenv ~/odoo-env
  . ~/odoo-env/bin/activate
  ./odoo.py

Actually when i was install Odoo on mac, i have so many problem so if you have problem you can write comment bellow and i will help you as i can.


Share:

Saturday, 16 July 2016

Encryption Description String with AES Python


https://dedyyuristiawan.blogspot.com


Hello world. Today i am gonna show you how to encryption and descryption string with AES. Today i've task to create link odoo with encryption, so automatically i must create decryption too. On this case i just use AES for encryption and decryption data, cause i think AES is the best algorithm encryption for now.

Here we go, this is my python function to encryption and decryption data. You can check it, if get wrong with my code. Lol

    def _default_key(self):
        default_key = b'no surprises guy'
        return default_key

    def _key(self, key):
        if len(key) >= 32:
            key = key[:32]
        else:
            key = self._pad(key)
        
        return key
    
    def bs(self):
        bs = 32
        return bs
    
    def _encrypt(self, raw, key):
        raw = self._pad(raw)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw))
    
    def _decrypt(self, enc, key):
        enc = base64.b64decode(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:]))
    
    def _pad(self, s):
        s = str(s)
        return s + (self.bs() - len(s) % self.bs()) * chr(self.bs() - len(s) % self.bs())
    
    def _unpad(self, s):
        s = str(s)
        return s[:-ord(s[len(s)-1:])]

Yes, to use this function you just call function _encrypt and _decrypt with the parameter like this.

    self._encrypt("Dedy Yuristiawan", self.key(self._default_key)) # parameter string and key
    self._decrypt(encrypted_data, self.key(self._default_key)) #parameter data encrypted and key

and for now don't forget to import library python on the top.

    from Crypto.Cipher import AES
    from Crypto import Random
    import base64
    from gdata.tlslite.utils.rijndael import encrypt

So the conclusion is use this function if you want to encrypt or decrypt data, if you confuse about my code, you can fill on the comment bellow, i hope this is can help you little bit.
Share:

Need Change GMT with Function Python


https://dedyyuristiawan.blogspot.co.id


Hello world. Today i am gonna show you, how to change time with function. Last post i just share how to show datetime field without wrong show, i just share without create function python. So today i gonna show you change Datetime field with function, sometimes after try any failed solution, we don't have choice to get great solution, we must create function to get great solution.

So for this post i gonna show you function python to change datetime with plus or minus requirement. This is my function python. 

def _add_hour(self, vdate):
    a_time = datetime.strptime(vdate, "%Y-%m-%d %H:%M:%S")
    a_time += timedelta(hours=7)
    b_time = a_time.strftime("%d/%m/%Y %H:%M:%S")
    
    return b_time

In this function i just change time with +7 hour, and after that i change format date like human know, not computer know. Lol.

For my last word, the conclusion is at least we must know how to get the purpose, whatever we chose another way, cause someday we certainly use it.
Share:

Thursday, 14 July 2016

How to show Datetime Field in Website / Front end Odoo


https://dedyyuristiawan.blogspot.co.id


Hello world. Today i've got problem with show field datetime in website. The problem is when you show datetime in website, date will show -7 hour,  +7 hour, etc. This problem is not bugs from Odoo,  cause in database Odoo data will save with GMT+0. Its not bugs, its a rule for the make our set datetime in anywhere easy. How can? yes when you show in backend is normal, datetime will show like your input, but when you want show in website is different, time will - 7 or something like that.

Ok lets make it clear. In this post i just want to show when i got problem with diatomite. Usually when we want to show datetime in website we use code like this. 

<tr t-foreach="split" t-as="line"> # looping model for get field
 <t t-set="no" t-value="no + 1"/>
 <td class="text-center"><span t-esc="no"/></td>
 <td class="text-center"><span t-esc="line.start_date"/></td>
 <td class="text-center"><span t-esc="line.end_date"/></td>
 <td class="text-center"><span t-esc="line.qty"/></td>
</tr>

line.end_date and line.start_date is my field. For the example in the backend i've input with 009-01-05 22:14:39, and when look into database time will save 009-01-05 22:07:39 its less 7 hour. Cause datetime will save with GMT+0. But why when in the backend will show 009-01-05 22:14:39 similar with i'd input in backend, Odoo will automatically convert time when you show in backend with this syntax <field/>. 

<field name="my_datetime"> #my_datetime is my datetime field in Odoo

so, its different when you show in website with syntax on the top or this syntax <span t-esc/>.

<span t-esc="line.end_date"/> #end_date is my datetime field in Odoo

datetime will show similar with database without automatically convert Odoo. In this case you don't need to change with javascript or make function python, you don't need this. Because Odoo have perfect syntax. This is it <span t-field="line.end_date"/> t-field. 

<span t-field="line.end_date"/> #end_date is my datetime field in Odoo. 

Perfect right, you don't need to make difficult to create function js or python you just need to use <t-field /> and datetime will show like your input (like what's you want). 

So the conclusion is when you want show Datetime field in backend you can use <field name="name_field"/> and different when you want show in website / front end you just use <t-field=name_field/> don't use the other. See yaa
Share:

Monday, 23 May 2016

Add field with Position Odoo


Hello world. Today i am gonna show you how to add custom field with position odoo. The last post i just share about add field odoo 'after'. In this post i will show you how implementation position add field odoo. Now i still use odoo 9 and module  'Dedy Custom Fields Product' . lets code !!

https://dedyyuristiawan.blogspot.co.id
View Product Odoo

1. Position = "before"
This is my form view product, now i will put my field before product type. i just use code like this. i will put my field before Product type.

<record id="add_field_product_form" model="ir.ui.view">  
     # For the tree view and id field (whatever)
      <field name="name">add.field.product.form</field>  
      # Name field (whatever)
      <field name="model">product.template</field>  
      # Parent object
      <field name="inherit_id" ref="product.product_template_only_form_view">
      # Parent View
      <field name="arch" type="xml">
      <xpath expr="//field[@name='type']" position="before">
  # Name field and Position (after)
        <field name="info">      
         # Your new Field
      </field></xpath>
      </field>
</field></record> 
similar like form view, i want change my tree view and just code like this 

<record id="add_field_product_tree" model="ir.ui.view">            
     # For the tree view and id field (whatever)
         <field name="name">add.field.product.tree</field>
         # Name field (whatever)
         <field name="model">product.template</field>  
         # Parent object
         <field name="inherit_id" ref="product.product_template_tree_view">
         # Parent View
         <field name="arch" type="xml">
     <xpath expr="//field[@name='type']" position="before">
     # Name field and Position (after)
       <field name="info">      
       # Your new Field
             </field></xpath>
         </field>
     </field></record>
and finally my form and tree view like this

https://dedyyuristiawan.blogspot.co.id
View Add Position Before



2. Position = "replace"

This is my form view product, now i will put my field before product type. i just use code like this. . i will put my field replace internal reference.

<record id="add_field_product_form" model="ir.ui.view">  
     # For the tree view and id field (whatever)
      <field name="name">add.field.product.form</field>  
      # Name field (whatever)
      <field name="model">product.template</field>  
      # Parent object
      <field name="inherit_id" ref="product.product_template_only_form_view">
      # Parent View
      <field name="arch" type="xml">
      <xpath expr="//field[@name='default_code']" position="replace">
  # Name field and Position (after)
        <field name="info">      
         # Your new Field
   </field></xpath>
      </field>
     </field></record> 
similar like form view, i want change standard price my tree view and just code like this 

<record id="add_field_product_tree" model="ir.ui.view">            
     # For the tree view and id field (whatever)
         <field name="name">add.field.product.tree</field>
         # Name field (whatever)
         <field name="model">product.template</field>  
         # Parent object
         <field name="inherit_id" ref="product.product_template_tree_view">
         # Parent View
         <field name="arch" type="xml">
     <xpath expr="//field[@name='standard_price']" position="replace">
     # Name field and Position (after)
       <field name="info">      
       # Your new Field
             </field></xpath>
         </field>
     </field></record>
and finally my form and tree view like this

https://dedyyuristiawan.blogspot.co.id
View Replace Field Odoo


And the conclusion is still like prev post you must know parent object, parent view, and position. you can use after, before, and replace. If you have any question just write comment, i will help if i can help you. Don't forget to be happy world. Thanks.
Share:

Tuesday, 17 May 2016

How to Add Field Odoo


https://dedyyuristiawan.blogspot.co.id/


Hello world. Today i am gonna show you " how to add field or inherit field Odoo ". Before we add field, i can tell what's Odoo? " Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management etc ", you can check in their web www.odoo.com . So first thing if you want to add field Odoo you must know What's inheritance? if you don't know about it, you can read this post. After that you can follow my steps.

In this tutorial i use odoo 9 community, and i will show you how to add field in product view. This is my view in product. And now i will add field after "Product Type" field. Just follow my steps.

https://dedyyuristiawan.blogspot.co.id
View Product Odoo


1. Activate the Developer Mode

Open help menu - about - activate the developer mode

https://dedyyuristiawan.blogspot.co.id
Activate Developer Mode


2. Find Field Name Want You Put your new Field

After you activate the developer mode, we can know name of the field, and the way to know name of filed is just put your cursor to field.

https://dedyyuristiawan.blogspot.co.id
Find Field Name

3. Find Parent Object and Parent View

Remember in this tutorial i will put my new field, after Product Type field. To find parent object and parent view just click Open debug menu (top right corner) - Edit form view and you will see view like this.

https://dedyyuristiawan.blogspot.co.id
View Edit Form View Odoo

As you can see product.template is parent object and product.product_template_only_form_view is parent view. After you know name field, parent object and parent view we can begin with the code.

4. Create Folder and 4 File inside the folder

i just create folder and give name dedy_custom_field, 4 file is __init__.py , __openerp__.py , add_field_product_view.xml , add_field_product.py.

__init__.py 

import add_field_product # Name file py

Its just import file py that your have created.

 __openerp__.py

{
    'name'          : 'Dedy Custom Fields Product',     # Name App
    'version'       : 'v.1.0',                                           # No Version
    'summary'       : 'Dedy Custom Fields Product', # Name Sumary
    'sequence'      : 1,                                                # No App
    'author'        : 'Dedy Yuristiawan',                      # Name Author
    'description'   : """                            
 
                        author : Dedy Y
                     
                        v.1.0 : \n
                        This module for add additional fields in Product\n
                     
         
                      """,                                                # Name description
    'category'      : 'field',                                      # Name category
    'depends'       : ['base','product'],                    # Name point parent
    'data'          : [
                        'add_field_product_view.xml',
                      ],                                                  # Name file xml
    'installable'   : True,                  
    'application'   : True,
    'auto_install'  : False
}
The point from this file is depends (from parent object), data (file xml), and name app.  

add_field_product.py

add_field_product.py

from openerp import tools
from openerp import models, fields, api
from pygments.lexer import _inherit

class AddFieldProduct (models.Model):# Name class
    _inherit = "product.template"    # Name parent object
 
    info = fields.Char('Info')       # Name field want you show to your xml with char type
The point from this file is name parent object and variable field

add_field_product_view.xml


<openerp>
    <data>
   
     <record model="ir.ui.view" id="add_field_product_tree">              
     # For the tree view and id field (whatever)
         <field name="name">add.field.product.tree</field>  
         # Name field (whatever)
         <field name="model">product.template</field>     
         # Parent object
         <field name="inherit_id" ref="product.product_template_tree_view"/>
         # Parent View
         <field name="arch" type="xml">
     <xpath expr="//field[@name='type']" position="after"> 
     # Name field and Position (after)
       <field name="info"/>         
       # Your new Field
             </xpath>
         </field>
     </record>
   
     <record model="ir.ui.view" id="add_field_product_form">    
     # For the tree view and id field (whatever)
      <field name="name">add.field.product.form</field>    
      # Name field (whatever)
      <field name="model">product.template</field>     
      # Parent object
      <field name="inherit_id" ref="product.product_template_only_form_view"/>
      # Parent View
      <field name="arch" type="xml">
      <xpath expr="//field[@name='type']" position="after"> 
  # Name field and Position (after)
        <field name="info"/>         
         # Your new Field
   </xpath>
      </field>
     </record>  
   
    </data>
</openerp>


 The point from this file is name (its whatever), model (from parent object), inherit_id (from parent view), Name field from odoo (@name='type') and name field that you already created.

After you already create 4 file and then put into folder addons odoo.

5. Update App List

App - Update App List

6. Find Your App

Apps - Apps

https://dedyyuristiawan.blogspot.co.id
View Apps Odoo


choose your app (Dedy Custom Field Product) that your already created. and then install.

7. See What's Happen

And now we done with this one, we success for add field into or modification Odoo. 

The conclusion is if you want to add field you must know parent object, parent view, and field nearest. After you know it, you can modification odoo. next post i will share about position want you put your field. 

If you have any question just write comment, i will help if i can help you. Don't forget to be happy world. Thanks
Share:


<br>


Advice for contacting me at dedyy54@gmail.com