Use [] to find tag! Example: [flutter, javascript]

Odoo ERP Development
Odoo ERP Development

Chapter 6

Exercise 1 - CRUD Without View

remove_red_eye 736 Times
spellcheck 609 Words, 3958 Characters
*Note: this book is still ongoing until it has a finished label.

After covering models, security models, list views, form views, action windows, and menu navigation, it's time to start exploring CRUD operations. For this tutorial, we will create CRUD functionality without utilizing form and list views by developing a new module called gepcode_prd. This module will document the requirements for a project. Let’s dive in!


Module Creation

The process begins by creating a custom addons folder.

1. Create a New Folder

In the root directory, create a folder to store your custom modules. For this example, we will name it gepcode.
Folder Addons

2. Configure Custom Addons

Update the addons_path in your configuration file to include the new folder:

[options]
addons_path = ./odoo/addons,./addons,./gepcode

3. Create a New Module Using CLI

Run the following command to scaffold a new module:

./odoo-bin scaffold gepcode_prd ./gepcode

This will generate the new module in the specified folder.
Module Folder


Creating the Model

1. Add a Model File

In the models folder, create a file named gepcode_prd.py and remove any placeholder files.
Model File

2. Update __init__.py

Modify the __init__.py file in the models folder to include the new model file:

from . import gepcode_prd

3. Define the Model

Add the following code to gepcode_prd.py to define the model:

from odoo import models, fields

class GepcodePRD(models.Model):
    _name = "gepcode_prd.prd"
    _description = "Gepcode PRD"

    name = fields.Char(string="Name", required=True)
    description = fields.Html(string="Description")

Configure Access Rights

Update the ir.model.access.csv file in the security folder to grant access to all internal users: Security Folder

Content of the file:

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_gepcode_prd_gepcode_prd,access_gepcode_prd_gepcode_prd x internal user,model_gepcode_prd_prd,base.group_user,1,1,1,1

Refer to the security model chapter for a detailed explanation.


Create Action Windows and Menu Navigation

To make the module accessible in the UI, follow these steps:

1. Create a View File

Add a file named gepcode_prd_views.xml in the views folder.
View File

2. Add Action and Menu

Include the following XML code in the view file:

<odoo>
    <data>
        <!-- Action -->
        <record id="gepcode_prd_action" model="ir.actions.act_window">
            <field name="name">PRD</field>
            <field name="res_model">gepcode_prd.prd</field>
            <field name="view_mode">list,form</field>
        </record>

        <!-- Menu -->
        <menuitem id="gepcode_prd_menu_root"
                  name="PRD"
                  sequence="1"
                  action="gepcode_prd_action" />
    </data>
</odoo>

Update the __manifest__.py File

Ensure the __manifest__.py file includes the necessary security and view files:

# -*- coding: utf-8 -*-
{
    'name': "Gepcode PRD",
    'summary': "Project Requirement Document Features",
    'description': "This module can be used to improve your team's productivity.",
    'license': 'AGPL-3',
    'author': "gepcode",
    'website': "https://gepcode.com",
    'category': 'Uncategorized',
    'version': '0.1',
    'depends': ['base', 'web'],
    'data': [
        'security/ir.model.access.csv',
        'views/gepcode_prd_views.xml',
    ],
    'demo': ['demo/demo.xml'],
}

Install the Module

Activate the Virtual Environment

Run the following commands to activate your virtual environment and start Odoo:

source venv/bin/activate
python odoo-bin -u odoo.conf

Enable Developer Mode

Go to Settings and click on Activate the developer mode.

Update App List

Navigate to the Apps menu, click on Update App List, and confirm the update.
Update App

Upgrade the Module

Search for gepcode_prd, remove any filters, and click Upgrade. Congratulations! You’ve successfully created CRUD functionality without views.

In the next chapter, we will explore CRUD with views. Stay tuned!


Next Article (List View)
Content Navigation