Creating Form Buttons and Actions

download Creating Form Buttons and Actions

of 24

Transcript of Creating Form Buttons and Actions

  • 8/19/2019 Creating Form Buttons and Actions

    1/64

    Creating form buttons and actions

    http://www.odoo.yenthevg.com/creating-form-buttons-and-actions/

    In this tutorial I will learn you how to create buttons in views. The form buttons are always shown at the

    top of the form and are very handy to perform actions on your current record. After this tutorial you can

    create your own buttons and actions behind the buttons.

    1. Creating your model and fields

     The rst step that you need to do is to create a model and add the elds you need. In my example I will

     just create a simple form with two elds named ‘name’ and ‘password’. So my model loos lie this!

    "#

    $%&'(

    ) *+* codin,! utf*- *+*from openerp import models elds api

     class button/action/demo0models.1odel2!  /name 3 4button.demo4  name 3 elds.5har0re6uired3Truedefault345lic on ,enerate name742  password 3 elds.5har02

     This creates a new model in the database named ‘button.demo’ and will contain two custom added elds!

    ‘name’ and ‘password’. The eld ‘name’ will be a re6uired eld and by default it will contain the text ‘5lic

    on ,enerate name7’2

  • 8/19/2019 Creating Form Buttons and Actions

    2/64

    2. Creating the view with buttons

    After the model elds have been made it is time to create your view. 8or this example I will create a simple

    form view that shows these two elds and I will add three buttons!

    "#

    $%&'(-9":"""#

    "$"%"&"'"("-

    ;record model3

  • 8/19/2019 Creating Form Buttons and Actions

    3/64

    "9#:#"

    hich will loo lie this!

    So how exactly does Bdoo now that the buttons need to be at the top of the formC >ecause of the header

    ta,7 Dverythin, within this ta, will be shown at the top of your form. So this creates three buttons but

    how do we tri,,er an action from this buttonC Eave a closer loo at the code for one of those buttons!

    ";button strin,3

  • 8/19/2019 Creating Form Buttons and Actions

    4/64

    3. Creating actions behind the buttons

    Fow that the view is built and we ,ave all buttons a name that maes the Hython ,o oJ we should create

    the Hython functions7 Kets rst create one for the ‘,enerate/record/name’ button!

    "#

    $%

    Lapi.onedef ,enerate/record/name0self2!

      )@enerates a random name between 9 and "& characters lon, and writes it to the record.  self.write0M4name4! 44.join0random.SystemNandom02.choice0strin,.ascii/uppercase O strin,.di,its2 for / in ran,e0randint09"&222P2

    self is denin, the current record so this means we can access anythin, from that record and modify it.

     The self.write will update the values for this record in the database. >y sayin, ‘name’! we are tellin, to

    update the eld ‘name’ in the view. In this example I created a simple function that ,enerates a random

    name between 9 and "& characters lon,.

    Tip: Lapi.one is used for one record and cannot be used for multiple records. If you’d want this you would

    need Lapi.multi.

    If you would now clic on the button a name would be ,enerated from this function and would be showndirectly on the view.

    Fow create the second function ‘,enerate/record/password’ for the button @enerate passwordG!

  • 8/19/2019 Creating Form Buttons and Actions

    5/64

    "#$%

    Lapi.onedef ,enerate/record/password0self2!  )@enerates a random password between "# and "& characters lon, and writes it to the record.  self.write0M4password4! 44.join0random.SystemNandom02.choice0strin,.ascii/uppercase O strin,.di,its2 for / in ran,e0randint0"#"&222P2

     This function does almost the same as the previous but it will ,enerate a password between "# and "&

    characters and write it on the eld ‘password’ in place of ‘name’.

    Fext lets create our third function ‘clear/record/data’ for the button ‘5lear data’. This function will clear all

    data from the elds!

    "

    #$%&'

    Lapi.one

    def clear/record/data0self2!  self.write0M  4name4! 44

     password4! 44  P2

     That is all7 Qou can now create buttons with actions and perform chan,es on the elds.

    4. Conclusion

  • 8/19/2019 Creating Form Buttons and Actions

    6/64

    >uttons are very handy in Bdoo and can do just about anythin,. It is easy to perform actions for the user

    chan,e elds show new views R

    Tip: Qou can also use buttons in combination with attrs and ows to mae them even more useful7 Qou can

    nd this tutorial here.o you want to try a demo module and see the source code of this tutorialC Qou can view on my @ithub

    account. Eas this tutorial helped you do you have any feedbac or 6uestionsC Host away7

    Creating and managing statusbars selections! in "doo

    In this tutorial I will learn you how to create and manage statusbars. Statusbars are selections (on database level) which

    are visually respresented by Odoo. They give you the ability to follow a specific flow and can be very handy to show you

    the progress that has been made. After this tutorial you can create and manage your own statusbars.

    In this example I will create a new form view which contains a statusbar and I’ll add buttons on the form which change the

    state of the record.

    1. Creating the model and fields

    The first step is to create a model (if you don’t have one yet) and to add a selection field:

    http://www.odoo.yenthevg.com/creating-and-managing-statusbars-selections-in-odoohttps://github.com/Yenthe666/Odoo_Samples/tree/master/button_action_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/button_action_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/button_action_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/button_action_demohttp://www.odoo.yenthevg.com/creating-and-managing-statusbars-selections-in-odoo

  • 8/19/2019 Creating Form Buttons and Actions

    7/64

    "

    #

    $

    %

    &

    '

    (

    -

    9

    "

    :

    "

    "

    "

    #

    "

    $

    "

    %

    class statusbar0models.1odel2!

      /name 3 4statusbar.demo4

      name 3 elds.5har04Fame4 re6uired3True2

     

  • 8/19/2019 Creating Form Buttons and Actions

    8/64

    This creates a new model with the name ‘statusbar.demo’ which contains two fields, the field ‘name’ and the field ‘state’.

    The state field is of the type selection, which means it will contain multiple values which can be chosen. A selection is

    always built up in the same way, the first part if the database name and the second part is the text that will be shown to the

    user. For example:

    " 04pro,ress4 4In pro,ress42

    If you wish to change the state to ‘In progress’ you will need to tell the database about ‘progress’, since this is the name

    the database listens to, while the name shown to the user will be ‘In progress’.

    This is all you need on the database level. You have your selection values in the database so you only need to show them

    to the user now!

    2. Creating the view 

    Now that you have the database part ready it is time to create your view. In this example I will create a simple form viewwhich shows the field ‘name’ and the statusbar with some buttons:

  • 8/19/2019 Creating Form Buttons and Actions

    9/64

    "

    #

    $

    %

    &

    '

    (

    -

    9

    ":

    "

    "

    "

    #

    "

    $

    "

    %

    "

    &

    "

    '

    "

    (

    ;record model3

  • 8/19/2019 Creating Form Buttons and Actions

    10/64

    "

    -

    "

    9

    #

    :

    #

    "

    #

    #

    #

    $

      ;?form=

      ;?eld=

    ;?record=

    So what does this tell us? Let us break this down part by part. The first thing you will notice is the field header.

    "

    #

    ;header=

    ;?header=

    Everything within these tags will be shown at the top of your Odoo form.

    In this code example it will create four buttons and it will also show the field ‘state’. The field ‘state’ has a widget set to it

    that is made by Odoo and which will make the typical statusbar, just by adding a widget! This will generate the following

  • 8/19/2019 Creating Form Buttons and Actions

    11/64

    bar at the top of your Odoo screen:

    Now there is one more thing I should explain, the buttons. By using those buttons we will modify in which state we are on

    the statusbar (selection) and we will control the whole flow. Let us take the button ‘In progress’ as an example to explain

    the code:

    ";button strin,3

  • 8/19/2019 Creating Form Buttons and Actions

    12/64

    Now that the view is ready and you have buttons they should also trigger something. Open up your Python file and create

    a function for all of those button names you have. Every button will have a separate Python function and will modify the

    current record with a write. In this tutorial I will create four functions that simply change the state of what the current record

    is in:

    "

    #

    $

    %

    &

    '

    (

    -

    9

    ":

    ""

    "#

    "$

    )This function is tri,,ered when the user clics on the button 4Set to concept4

    Lapi.one

    def concept/pro,ressbar0self2!

      self.write0M

      4state4! 4concept4

      P2

     

    )This function is tri,,ered when the user clics on the button 4Set to started4

    Lapi.one

    def started/pro,ressbar0self2!

      self.write0M

    4state4! 4started4

      P2

  • 8/19/2019 Creating Form Buttons and Actions

    13/64

    "%

    "&

    "'

    "(

    "-

    "9

    #:

    #"

    ##

    #$

    #%

    #&

    #'

    #(

     

    )This function is tri,,ered when the user clics on the button 4In pro,ress4

    Lapi.one

    def pro,ress/pro,ressbar0self2!

      self.write0M

    4state4! 4pro,ress4

      P2

     

    )This function is tri,,ered when the user clics on the button 4one4

    Lapi.one

    def done/pro,ressbar0self2!

      self.write0M

    4state4! 4nished4

      P2

    When you now click on the button ‘In progress’ it will trigger the function ‘progress_progressbar’. The self.write will be

    triggered, which is telling Odoo that it should write on the current record, and it will modify the state to ‘finished’.

    Depending on in which state you are you’ll see buttons showing up and dissapearing. This is controller by the attrs value

  • 8/19/2019 Creating Form Buttons and Actions

    14/64

    on the buttons. For example when I save my record and set it to in progress:

    4. Conclusion

    Statusbars (selections) are very handy in Odoo and give you the ability to show the progress to the user in a visual way.

    Statusbars are ideal to use in combination with buttons which will modify the state where a record is in. Another great

    thing about statusbars is that they’re always at the exact same location and you can find out what the progress is within

    seconds.

    Do you want to try a demo module and see the source code of this tutorial? Qou can view on my @ithub account.

    Has this tutorial helped you, do you have any feedback or questions? Post away!

    https://github.com/Yenthe666/Odoo_Samples/tree/master/statusbar_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/statusbar_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/statusbar_demo

  • 8/19/2019 Creating Form Buttons and Actions

    15/64

    Creating many2many drag and drop handle! widget in "doo

    In this tutorial I will learn you how to create a handle wid,et. A handle wid,et ,ives you the option to re*

    order lines in a many#many main, records dra, and droppable. This is a very hand feature to order

    items and eventually chan,e the order on reports for example.

    An example from before re*arran,in, a many#many and after!

    1. Creating the model and fields

    8or a dra, and drop 0handle2 wid,et you will need a many#many and for havin, a many#many you need to

    create a lin to another table. The rst step is to create a new table in case you don’t already have one or

    otherwise you can sip to part #. The table where you want to lin your many#many to needs atleast one

    eld 0such as name2 and what you absolutely need is a eld named ‘se6uence’. The se6uence eld is an

    inte,er and will eep trac of the order on your many#many. 8or example!

  • 8/19/2019 Creating Form Buttons and Actions

    16/64

    "

    #

    $

    %

    &

    '

    class many#many/default/data/demo0models.1odel2!

      /name 3 4sale.order.handle4

      /description 3 41odel for many#many 0handle24

      /order 3 4se6uence4

      name 3 elds.5har04Faam42

      se6uence 3 elds.Inte,er04se6uence4 help3

  • 8/19/2019 Creating Form Buttons and Actions

    17/64

    "

    #

    $

    %

    &

    '

    class print/order/sample0models.1odel2!

      def /,et/default/print/order/ids0self2!

      return self.pool.,et04sale.order.handle42.search0self.env.cr self.env.uid UV2

     

     /inherit 3 4sale.order4

      print/handle/ids 3 elds.1any#many04sale.order.handle44sale/handle44order/id44order/handle/id441any#many order4help34This could be

    used to create a print order for your report for example.This is a dra, an drop 0handle2 wid,et.4default3/,et/default/print/order/ids2

    As you can see I’ve inherited the model sale.order since I want to create my many#many in this model. I

    then created a new eld and created a lin to the table sale.order.handle. I’ve also created a default which

    lins to a function. The idea behind this is to automatically ll the many#many handle with all the datafrom the model sale.order.handle. Bfcourse you don’t have to this is just for the demo.

    Tip: But of safety uses you should never create a table lon,er than "' characters in a many#many7 The

    post,reSWK database cannot handle a many#many lin lon,er than '% characters in total. 8or more

    information see this bu, report.

    3. Creating the view 

    Fow that you’ve created both your model and the many#many you only need to add the many#many toyour view. The code will loo lie this!

    https://github.com/odoo/odoo/issues/2780#issuecomment-120707391https://github.com/odoo/odoo/issues/2780#issuecomment-120707391https://github.com/odoo/odoo/issues/2780#issuecomment-120707391https://github.com/odoo/odoo/issues/2780#issuecomment-120707391

  • 8/19/2019 Creating Form Buttons and Actions

    18/64

    "

    #

    $

    %

    &

    '

    (

    -

    9

    ":

    ""

    "#

    "$

    "%

    "&

    "'

    "(

    "-

    "9

    #:

    ;record id3

  • 8/19/2019 Creating Form Buttons and Actions

    19/64

     There isn’t to much diJerence with a normal many#many there are just three thin,s you should always

    do.

    ". 5reate your own tree to render the many#many correctly.

    #. always add the eld ‘se6uence’ in this tree.$. call the wid,et with wid,et3GhandleG. This will mae the many#many dra, and droppable.

    And thats it you’ve created your own many#many handle which maes items dra, and droppable. The

    result will loo somethin, lie this!

  • 8/19/2019 Creating Form Buttons and Actions

    20/64

    o you want to see the source code or try this module for yourselfC Qou can download ? view it on @ithub.

    Eas this tutorial helped you do you have any feedbac or 6uestionsC Host away7

    https://github.com/Yenthe666/Odoo_Samples/tree/master/many2many_handle_widget_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/many2many_handle_widget_demo

  • 8/19/2019 Creating Form Buttons and Actions

    21/64

    $path e%pressions in "doo &

    In this tutorial I will learn you how to use xpath expressions. Xpath expressions ,ive you the ability to add

    new pa,es ,roups elds R to an already existin, view. hen you inherit a currently existin, view in a

    module and want to extend it you will absolutely need xpath expressions. In this sample I will add new

    pa,es ,roups and elds to the view under sales = products.

    Tip:view my sample xpath module.

    1. Create new fields

     The rst step is to create new elds inside your model. 8or example!

    "

    #

    $

    %

    &

    '

    ) *+* codin,! utf*- *+*

     

    from openerp import models elds api

     

    class on/chan,e/function0models.1odel2!

      )Inhertis the model product.template

    https://github.com/Yenthe666/Odoo_Samples/tree/master/xpath_expressionshttps://github.com/Yenthe666/Odoo_Samples/tree/master/xpath_expressions

  • 8/19/2019 Creating Form Buttons and Actions

    22/64

    (

    -

    9

    ":

    "

    "

    "

    #

      /inherit 3 4product.template4

      )5reates two new elds 05ostHrice and Shippin,5ost2 in the model product.template

      5ostHrice 3 elds.8loat04>uy price42

      Shippin,5ost 3 elds.8loat04Shippin, 5ost42

      8ieldAfter@roup 3 elds.5har0strin,348ield After @roup42

      8ieldFewHa,e 3 elds.5har0strin,348ield Few Ha,e42

    2. 'nherit e%isting view 

    hen you’ve created your elds you need to show them. In this example I will inherit the view from the

    product module which is showed under sales = products. Qou can reference another view with the ref3GG

    ta,. The rst step is to nd the name of the view you want and then addin, the module name in front of it.

    8or example!

    "

    #

    $

    ;record id3

  • 8/19/2019 Creating Form Buttons and Actions

    23/64

    % ;eld name3

  • 8/19/2019 Creating Form Buttons and Actions

    24/64

     3. 1 XP AT H PA GE 

    "

    #

    $

    %

    &

    '

    (

    ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    25/64

    hich will result in the followin, pa,e!

     3. 2 XP ATH GR OU P 

    "

    #

    $

    ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    26/64

    %

    &

      ;?,roup=

    ;?xpath=

     This code will create a new ,roup after the ,roup inside the pa,e with as title ‘Information’ and will showthe eld ‘8ieldAfter@roup’ in it.

    hich will result in the followin, pa,e!

  • 8/19/2019 Creating Form Buttons and Actions

    27/64

     3. 3 XP ATH FI EL DS 

    "

    #

    $

    %

    ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    28/64

    Tip: there are three options for the position ta,!

    " ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    29/64

    #

    $

    ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    30/64

    #. Bpen up the folder views and then open the X1K leim_odoo_support.xml. In this le you can directly

    see that there is a function and it loos to Bdoo Support. So ,o ahead and comment this bloc out lie

    this!

    "

    #

    $

    %

    &

    ;Cxml version3

  • 8/19/2019 Creating Form Buttons and Actions

    31/64

    '

    (

    -

    9

    "

    :

    "

    "

    "

    #

    "

    $

    "

    %

    "

    &

    "

    '

    "

    (

    "

    -

    "

    9

    #

    :

      ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    32/64

    #. @o to im/odoo/support?static?src?xml? and open the leim_odoo_support.xml. Qou’ll see an li class

    here for Bdoo Support too. 5omment it out lie this!

    "

    #

    $

    %

    &

    '

    (

    -

    9

    ":

    ""

    "#

    ;Cxml version3

  • 8/19/2019 Creating Form Buttons and Actions

    33/64

    %. Bpen Bdoo in your browser and you’ll see there is no more ‘Bdoo Support’ user in the list7

    'nheriting and modifying ()eb reports

    In this tutorial you will learn how to inherit already existin, Web reports in a custom module. After thistutorial you will be able to inherit and modify any already existin, Web report in Bdoo7

    In this example I will modify the default Wuotation ? order report throu,h inheritance and I will remove a

    few elements from the report.

    1. Creating a new module

     The rst step is to create a new module. The correct behaviour in Bdoo is to always inherit don’t ever

    modify existin, les. Start by creatin, yourself a new module.Tip: use the scaJold command from Bdoo7

     This will create a new module from scratch and the default structure of your module is already there.

  • 8/19/2019 Creating Form Buttons and Actions

    34/64

    2. Creating your $*+ file

     The next step is to open up your X1K le 0in my example templates.xml2 and to create a new record to

    inherit the report. To inherit a report you have to now in which module it is and what the ori,inal X1K id of 

    the report is. So how do you nd thisC

     The easiest way is to ,o to ‘Settin,s’ = ‘Neports’ = ‘Neports’ and to search the report you want to modify.

    In this tutorial this will be the 6uotation?order report so I’ll search for ‘Brder’!

  • 8/19/2019 Creating Form Buttons and Actions

    35/64

    Fow that you’ve found your report clic on it. This will open a new view where you will nd all the technical

    information you need7 8or example with the 6uotation report!

    At the ri,ht you will see a clicable lin named ‘Search associated Web views’. 5lic on it. This will show

    you a list of all records that are associated to this specic report. In the example of the 6uotation report!

    So this usually shows you two X1K records. Eow do you now which one you needC The one that ends with

     /document is the correct X1K record that you need to inherit. Znder the column ‘Dxternal I’ you will see

    there is an uni6ue name in this example sale.report/saleorder/document. The rst part of this text 0sale2

  • 8/19/2019 Creating Form Buttons and Actions

    36/64

    is the module where the report is from the second part 0report/saleorder/document2 is the name of the

    report that you want to inherit and modify.

    Nemember this value and now open up your X1K le. To inherit a Web report you need two thin,s! an

    uni6ue template id and the inherit/id. The template id can be chosen by yourself just mae sure its

    uni6ue. The inherit/id should contain the value you’ve just found on your report

    0sale.report/saleorder/document2.

    "

    #

    $

    ;7** Inherit 6uotation report 0from module sale2 **=

    ;template id3

  • 8/19/2019 Creating Form Buttons and Actions

    37/64

    "

    #

    $

    %

    &

    '

    (

    -

    ;7** 8inds the rst table with as class table table*condensed and ,ives the ability to modify it

     This will replace everythin, within, tr 0includin, tr2**=

    ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    38/64

    $

    %

    &

    '

    ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    39/64

    9

    "

    :

    "

    "

    "

    #

    "

    $

    "

    %

    "

    &

    "

    '

    "

    (

    "

    -

    "

    9

    #

    :

    #

    "

      ;th=escription;?th=

      ;th class3

  • 8/19/2019 Creating Form Buttons and Actions

    40/64

    If you would now print out the report you would ,et this as a result!

    3. #dding the dependency for the e%ternal ()eb report

     The next and nal step is to add a dependency. >ecause this Web report is inside another module Bdoo

    has to now about this module and its content so you should add a dependency. ithout this your modulewill not wor and you will ,et errors.

    Bpen up your //openerp//.py le in your custom module and nd the line with depends.

    Fow tae bac that ‘Dxternal I’ from the Web report and tae the rst part of the external id 0before the

    dot2. This tells you which module you should add as a dependency!

  • 8/19/2019 Creating Form Buttons and Actions

    41/64

    In this example my Web comes from the sale module so I will add it as a dependency.

    "

    #

    ) any module necessary for this one to wor correctly

    4depends4! U4sale4V

    4. Conclusion

     Thats it7 Qou’re done with inheritin, and modifyin, the Web report. hen you now install this module inyour Bdoo you will see the modied report.

    o you want to try a demo module and see the source code of this tutorialC Qou can view on my @ithub

    account.

    https://github.com/Yenthe666/Odoo_Samples/tree/master/inherit_report_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/inherit_report_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/inherit_report_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/inherit_report_demo

  • 8/19/2019 Creating Form Buttons and Actions

    42/64

    on,change event in "doo &

    In this tutorial I will learn you how to create an on/chan,e event that automatically updates a eld when

    another eld chan,es. In this sample I will create two new elds under sales = products in the tab

    ‘Hrocurements’. hen a value chan,es on one of the two elds a third eld will be updated with the total

    of the other two elds.

    1. Creating new fields

     The rst step is to create new elds in the Hython le. 8or example!

    "

    #

    $

    %

    &

    '

    (

    ) *+* codin,! utf*- *+*

     

    from openerp import models elds api

     

    class on/chan,e/function0models.1odel2!

      )Inhertis the model product.template

      /inherit 3 4product.template4

  • 8/19/2019 Creating Form Buttons and Actions

    43/64

    -

    9

    "

    :

      )5reates two new elds 05ostHrice and Shippin,5ost2 in the model product.template

      5ostHrice 3 elds.8loat04>uy price42

      Shippin,5ost 3 elds.8loat04Shippin, 5ost42

     This adds two elds 05ostHrice and Shippin,5ost2 to the model product.template which I rst inherited.

    >oth these elds are for numbers. After addin, your new elds you need to add them to the view.

    2. #dd fields to view 

    In case you’re codin, in the current module you can simply add the elds lie this!

    "

    #

      ;eld name3

  • 8/19/2019 Creating Form Buttons and Actions

    44/64

    "

    #

    $

    %

    &

    '

    (

    -

    9

    "

    :

    "

    "

    "

    #

    "

    $

    "

    %

    ;openerp=

      ;data=

      ;record id3

  • 8/19/2019 Creating Form Buttons and Actions

    45/64

    "

    &

    3. "n,change function

    Foticin, anythin, in this codeC I’ve already added the on/chan,e event in the view lie this!

    " on/chan,e3

  • 8/19/2019 Creating Form Buttons and Actions

    46/64

    $

    %

    &

    '

    (

    -

    9

    "

    :

    "

    "

    "

    #

    )5alculate the total

    total 3 5ostHrice O Shippin,5ost

      res 3 M

      4value4! M

    )This sets the total price on the eld standard/price.

      4standard/price4! total

      P

    P

    )Neturn the values to update it in the view.

    return res

    In the variables 5ostHrice and Shippin,5ost are the values that the user has entered. The variable total

    simply contains the sum of 5ostHrice and Shippin,5ost and this should now be lled in to our third eld

    which is named standard/price. After the value is calculated and added to the new eld it should be

    returned so that the user can see it.

  • 8/19/2019 Creating Form Buttons and Actions

    47/64

     The result!

    "#$

    %

    )Import lo,,erimport lo,,in,)@et the lo,,er

     /lo,,er 3 lo,,in,.,etKo,,er0//name//2

    2. sing logging to print/log statements

    After importin, lo,,in, you can directly start writin, lo, statements. There are ve diJerent states for

    lo,,in, pre*built!

    •   logger.debug! for debu,,in, purposes.

    •   logger.info! for showin, information.

    •   logger.warning! for showin, minor issues.

    •   logger.error! for showin, that an operation has failed

    •   logger.critical! for showin, that somethin, has ,one horribly wron,. In Bdoo these will be used to

    notify a module doesn’t wor anymore.

  • 8/19/2019 Creating Form Buttons and Actions

    48/64

  • 8/19/2019 Creating Form Buttons and Actions

    49/64

    hen you clic on the button the lo,,in, statements will be printed in your terminal. 8or example with my

    function!

     Thats it7 It is as simple as that to lo, data in Bdoo. If you want to ,o further thou,h there are 6uite some

    extra options.

    3. #dvanced logging configuration

    Bdoo ,oes a bit further than this thou,h7 Qou can con,ure multiple lo,,in, options in Bdoo!

    •   logle! the lo, lename. If this is not lled in all the lo, details will be written to stdout 0terminal2.

  • 8/19/2019 Creating Form Buttons and Actions

    50/64

    •   log_level! any value in the list of U‘debu,/rpc/answer’ ‘debu,/rpc’ ‘debu,’ ‘debu,/s6l’ ‘info’

    ‘warn’ ‘error’ ‘critical’V. Bdoo chan,ed the lo,/level meanin, here because these level values are

    mapped to a set of predened ‘module!lo,/level’ pairs.

    •   log_handler! can be a list of ‘module!lo,/level’ pairs. The default value is ‘!IF8B’ [ it means the

    default lo,,in, level is ‘IF8B’ for all modules.

    •   logrotate! True?8alse. If True create a daily lo, le and eep $: les.

    •   log_db! True?8alse. If this is set to ‘True’ the lo,,in, will also be written to the ‘ir/lo,,in,’ table in the

    database.

    8or example if I dene a lo,le in the terminal!

    hen you now ,o to this location you will see there is a new lo,le created and that it is automatically

    lled with the data that you dened!

    o you want to see the source code or try this module for yourselfC Qou can download ? view it on @ithub.

    Eas this tutorial helped you do you have any feedbac or 6uestionsC Host away7

    https://github.com/Yenthe666/Odoo_Samples/tree/master/logging_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/logging_demo

  • 8/19/2019 Creating Form Buttons and Actions

    51/64

    5reatin, many#many dra, and drop 0handle2 wid,et in Bdoo \ZKQ "# #:"& QDFTED''' KDA]D A 5B11DFT

    Ei ,uys

    In this tutorial I will learn you how to create a handle wid,et. A handle wid,et ,ives you the option to re*

    order lines in a many#many main, records dra, and droppable. This is a very hand feature to order

    items and eventually chan,e the order on reports for example.

    An example from before re*arran,in, a many#many and after!

    1. Creating the model and fields

    8or a dra, and drop 0handle2 wid,et you will need a many#many and for havin, a many#many you need to

    create a lin to another table. The rst step is to create a new table in case you don’t already have one or

    http://www.odoo.yenthevg.com/creating-many2many-drag-and-drop-handle-widget-in-odoo/http://www.odoo.yenthevg.com/creating-many2many-drag-and-drop-handle-widget-in-odoo/http://www.odoo.yenthevg.com/author/yenthe666/http://www.odoo.yenthevg.com/creating-many2many-drag-and-drop-handle-widget-in-odoo/#respondhttp://www.odoo.yenthevg.com/creating-many2many-drag-and-drop-handle-widget-in-odoo/http://www.odoo.yenthevg.com/creating-many2many-drag-and-drop-handle-widget-in-odoo/http://www.odoo.yenthevg.com/author/yenthe666/http://www.odoo.yenthevg.com/creating-many2many-drag-and-drop-handle-widget-in-odoo/#respond

  • 8/19/2019 Creating Form Buttons and Actions

    52/64

    otherwise you can sip to part #. The table where you want to lin your many#many to needs atleast one

    eld 0such as name2 and what you absolutely need is a eld named ‘se6uence’. The se6uence eld is an

    inte,er and will eep trac of the order on your many#many. 8or example!

    "#$%

    &'

    class many#many/default/data/demo0models.1odel2!  /name 3 4sale.order.handle4  /description 3 41odel for many#many 0handle24  /order 3 4se6uence4

      name 3 elds.5har04Faam42  se6uence 3 elds.Inte,er04se6uence4 help3

  • 8/19/2019 Creating Form Buttons and Actions

    53/64

    "#$%&'

    class print/order/sample0models.1odel2!  def /,et/default/print/order/ids0self2!  return self.pool.,et04sale.order.handle42.search0self.env.cr self.env.uid UV2 

     /inherit 3 4sale.order4  print/handle/ids 3 elds.1any#many04sale.order.handle44sale/handle44order/id44order/handle/id441any#many order4help34This could beused to create a print order for your report for example.This is a dra, an drop 0handle2 wid,et.4default3/,et/default/print/order/ids2

    As you can see I’ve inherited the model sale.order since I want to create my many#many in this model. I

    then created a new eld and created a lin to the table sale.order.handle. I’ve also created a default which

    lins to a function. The idea behind this is to automatically ll the many#many handle with all the data

    from the model sale.order.handle. Bfcourse you don’t have to this is just for the demo.

    Tip: But of safety uses you should never create a table lon,er than "' characters in a many#many7 The

    post,reSWK database cannot handle a many#many lin lon,er than '% characters in total. 8or more

    information see this bu, report.

    3. Creating the view 

    Fow that you’ve created both your model and the many#many you only need to add the many#many to

    your view. The code will loo lie this!

    "#$%&'

    ;record id3

  • 8/19/2019 Creating Form Buttons and Actions

    54/64

    (-9":"""#"$"%"&"'"("-"9#:

      ;xpath expr3

  • 8/19/2019 Creating Form Buttons and Actions

    55/64

    result will loo somethin, lie this!

    o you want to see the source code or try this module for yourselfC Qou can download ? view it on @ithub.

    Eas this tutorial helped you do you have any feedbac or 6uestionsC Host away7

    Automatically ll 1any#many in Bdoo

    https://github.com/Yenthe666/Odoo_Samples/tree/master/many2many_handle_widget_demohttp://www.odoo.yenthevg.com/automatically-fill-many2many-in-odoo/https://github.com/Yenthe666/Odoo_Samples/tree/master/many2many_handle_widget_demohttp://www.odoo.yenthevg.com/automatically-fill-many2many-in-odoo/

  • 8/19/2019 Creating Form Buttons and Actions

    56/64

  • 8/19/2019 Creating Form Buttons and Actions

    57/64

    "#$%&'(-

    class print/order/sample0models.1odel2!  def /,et/default/print/order/ids0self2!

    cr 3 self.pool.cursor02self.env

      return self.pool.,et04sale.order.printorder42.search0cr self.env.uid UV2 

     /inherit 3 4sale.order4  print/order/ids 3 elds.1any#many04sale.order.printorder44sale/order/print44print/id44order/print/id44Hrint order4help34This could be usedto create a print order for your report for example.4default3/,et/default/print/order/ids2

    As you can see there is a default3/,et/default/print/order/ids which lins to a function in the class. This

    function will automatically ,et all data from the model sale.order.printorder thans to self.pool. After thepool ,ot all records we will return all the data and it will be added to the many#many.

    Fow lets create a view that shows the many#many!

    "#$%&'(-9

    ;record id3

  • 8/19/2019 Creating Form Buttons and Actions

    58/64

    ":"""#"$"%

      ;eld name3

  • 8/19/2019 Creating Form Buttons and Actions

    59/64

    "#$%&'(-9":

    """#"$"%"&"'"("-"9#:#"##

    ;Cxml version3

  • 8/19/2019 Creating Form Buttons and Actions

    60/64

    #$#%#&#

    '#(

    TIP: Add the lename defaultdata.xml to //openerp//py under ‘data’.

    hen the module is installed these records will be automatically created in the database and will never be

    updated thans to the noupdate3G"^. hen you need a many#many lled with default data that always

    has to be the same this is perfect. If this is not the case simply create a new view where users can create

    new records in to the model sale.order.printorder7

    o you want to see the source code or try this module outC Qou can download ? view it on @ithub7

    Eas this tutorial helped you do you have any feedbac or 6uestionsC Host away7

    Installin, default data in Bdoo moduleIn this tutorial I will learn you how to install data in a module by default. This will mean that data is

    automatically inserted in the database whenever you would install this module. In some cases this is very

    handy because you need some default values inserted when the module is installed.

    1. #dding a new $*+ file to the module

     The rst thin, you need to do is create a new X1K le in your custom module for example demodata.xml

    and to add the lename to //openerp//.py. An example of my //openerp//.py le!

    https://github.com/Yenthe666/Odoo_Samples/tree/master/many2many_default_data_demohttp://www.odoo.yenthevg.com/installing-default-data-in-odoo-module/https://github.com/Yenthe666/Odoo_Samples/tree/master/many2many_default_data_demohttp://www.odoo.yenthevg.com/installing-default-data-in-odoo-module/

  • 8/19/2019 Creating Form Buttons and Actions

    61/64

    "#$%&'(-9":

    """#"$"%"&"'"(

    "-"9#:#"##

    ) *+* codin,! utf*- *+*M  4name4!

  • 8/19/2019 Creating Form Buttons and Actions

    62/64

    #$#%#&#

    '#(#-#9$:$"$#

    $$$%$&

    P

    Create default data

    After you’ve added the location to the le 0to //openerp//.py2 you should create the data for the le. Bpen

    up your custom module ,o in to the folder data and edit your X1K le. The structure is always the same. Qou ,ive the record a name 0this can be anythin, mae sure its uni6ue2 and add the name of the model

    where you want data installed in. Then add a eld for every eld you have in the model which you want to

    ll with data. The followin, is an example that creates a record in the model ‘demo.default.data’ when you

    install the module!

  • 8/19/2019 Creating Form Buttons and Actions

    63/64

    "#$%

    ;record id3

  • 8/19/2019 Creating Form Buttons and Actions

    64/64

    hen you would now install this module it will automatically install the records that are dened.

    ant to see an example module where you can see how everythin, worsC Qou can download?view it on

    @ithub7

    Eas this tutorial helped you do you have any feedbac or 6uestionsC Host away7

    https://github.com/Yenthe666/Odoo_Samples/tree/master/default_data_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/default_data_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/default_data_demohttps://github.com/Yenthe666/Odoo_Samples/tree/master/default_data_demo