cundi / blog

push issues as a blog
2 stars 0 forks source link

创建Odoo中的按钮和动作[翻译] #43

Open cundi opened 8 years ago

cundi commented 8 years ago

原文链接:http://www.odoo.yenthevg.com/category/odoo-8/buttons/

Creating form buttons and actions

OCTOBER 10, 2015YENTHE666 4 COMMENTS

Hi guys,

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 first step that you need to do is to create a model and add the fields you need. In my example I will just create a simple form with two fields named ‘name’ and ‘password’. So my model looks like this:

# -*- coding: utf-8 -*-
from openerp import models, fields, api

class button_action_demo(models.Model):
    _name = 'button.demo'
    name = fields.Char(required=True,default='Click on generate name!')
    password = fields.Char()

This creates a new model in the database named ‘button.demo’ and will contain two custom added fields: ‘name’ and ‘password’. The field ‘name’ will be a required field and by default it will contain the text ‘Click on generate name!’)

2. Creating the view with buttons

After the model fields have been made it is time to create your view. For this example I will create a simple form view that shows these two fields and I will add three buttons:

<record model="ir.ui.view" id="view_buttons_form">
    <field name="name">Buttons</field>
    <field name="model">button.demo</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Button record">
        <!--The header tag is built to add buttons within. This puts them at the top -->
        <header>
        <!--The oe_highlight class gives the button a red color when it is saved.
        It is usually used to indicate the expected behaviour. -->
            <button string="Generate name" type="object" name="generate_record_name" class="oe_highlight"/>
        <button string="Generate password" type="object" name="generate_record_password"/>
        <button string="Clear data" type="object" name="clear_record_data"/>
        </header>
        <group>
        <field name="name"/>
        <field name="password"/>
        </group>
    </form>
    </field>
</record>

Which will look like this:

Form view example

So, how exactly does Odoo know that the buttons need to be at the top of the form? Because of the header tag! Everything within this tag will be shown at the top of your form. So, this creates three buttons, but how do we trigger an action from this button? Have a closer look at the code for one of those buttons:

<button string="Generate name" type="object" name="generate_record_name" class="oe_highlight"/>

Noticing anything? The type=”object” is telling us that we have an object and the name=”generate_record_name” is a link to a Python function. When the user clicks on the button “Generate name” Odoo will trigger the Python function ‘generate_record_name’ and from there on we can do whatever we want. So let us create the actions for the buttons!

3. Creating actions behind the buttons

Now that the view is built and we gave all buttons a name that makes the Python go off we should create the Python functions! Lets first create one for the ‘generate_record_name’ button:

@api.one
def generate_record_name(self):
    #Generates a random name between 9 and 15 characters long and writes it to the record.
    self.write({'name': ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(randint(9,15)))})

self is defining the current record, so this means we can access anything from that record and modify it. The self.write will update the values for this record in the database. By saying ‘name’: we are telling to update the field ‘name’ in the view. In this example I created a simple function that generates a random name between 9 and 15 characters long.

Tip:

@api.one is used for one record and cannot be used for multiple records. If you’d want this you would need @api.multi.

If you would now click on the button a name would be generated from this function and would be shown directly on the view.

Now create the second function ‘generate_record_password’ for the button “Generate password”:

@api.one
def generate_record_password(self):
    #Generates a random password between 12 and 15 characters long and writes it to the record.
    self.write({'password': ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(randint(12,15)))})

This function does almost the same as the previous but it will generate a password between 12 and 15 characters and write it on the field ‘password’ in place of ‘name’.

Next, lets create our third function ‘clear_record_data’ for the button ‘Clear data’. This function will clear all data from the fields:

@api.one
def clear_record_data(self):
    self.write({
        'name': '',
     password': ''
    })

That is all! You can now create buttons with actions and perform changes on the fields.

4. Conclusion

Buttons are very handy in Odoo and can do just about anything. It is easy to perform actions for the user, change fields, show new views, …

Tip: You can also use buttons in combination with attrs and flows to make them even more useful! You can find this tutorial here.

Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account. Has this tutorial helped you, do you have any feedback or questions? Post away!