Creating an application model

Now that Odoo knows about our new module, let's start by adding to it a simple model.

Models describe business objects, such as an opportunity, a sales order, or a partner (customer, supplier, and so on.). A model has a list of attributes and can also define its specific business.

Models are implemented using a Python class derived from an Odoo template class. They translate directly to database objects, and Odoo automatically takes care of that when installing or upgrading the module.

Some consider it good practice to keep the Python files for models inside a models subdirectory. For simplicity we won't be following that here, so let's create a todo_model.py file in the todo_app module main directory.

Add the following content to it:

# -*- coding: utf-8 -*-
from openerp import models, fields
class TodoTask(models.Model):
    _name = 'todo.task'
    name = fields.Char('Description', required=True)
    is_done = fields.Boolean('Done?')
    active = fields.Boolean('Active?', default=True)

The first line is a special marker telling the Python interpreter that this file has UTF-8, so that it can expect and handle non-ASCII characters. We won't be using any, but it's safer to use it anyway.

The second line makes available the models and fields objects from the Odoo core.

The third line declares our new model. It's a class derived from models.Model. The next line sets the _name attribute defining the identifier that will be used throughout Odoo to refer to this model. Note that the actual Python class name is meaningless to the other Odoo modules. The _name value is what will be used as an identifier.

Notice that this and the following lines are indented. If you're not familiar with Python you should know that this is important: indentation defines a nested code block, so these four line should all be equally indented.

The last three lines define the model's fields. It's worth noting that name and active are names of special fields. By default Odoo will use the name field as the record's title when referencing it from other models. The active field is used to inactivate records, and by default only active records will be shown. We will use it to clear away completed tasks without actually deleting them from the database.

Right now, this file is not yet used by the module. We must tell Odoo to load it with the module in the __init__.py file. Let's edit it to add the following line:

from . import todo_model

That's it. For our changes to take effect the module has to be upgraded. Locate the To-Do application in the Local Modules and click on its Upgrade button.

Now we can inspect the newly created model in the Technical menu. Go to Database Structure | Models and search for the todo.task model on the list. Then click on it to see its definition:

Creating an application model

If everything went right, this will let us confirm that the model and our fields were created. If you made changes and don't see them here, try a server restart, as described before, to force all of the Python code to be reloaded.

We can also see some additional fields we didn't declare. These are the five reserved fields Odoo automatically adds to any model. They are as follows:

  • id: This is the unique identifier for each record in the particular model.
  • create_date and create_uid: These tell us when the record was created and who created it, respectively.
  • write_date and write_uid: These tell us when the record was last modified and who modified it, respectively.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset