Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Create new module and new table in database

By
Anchal
on 3/24/16, 4:19 AM 861 views

Hello,

I can create new module by inheriting other models and fields data saved in existing table but I want to create new module which data will save in new table in database.

can anyone help me or give me a sample model please.

2
Dr Obx
On 3/24/16, 6:04 AM

To create new module you must:

1. Create a folder in Addons

2. Create __init__.py

3. Create __openerp__.py

4. Create your module.py file

5. Create your module_view.xml file

6. Security folder and inside of this folder ir.model.access.csv where you can put some information about access etc

__init__.py should contain: import your_module/s_name/s

example:

# -*- coding: utf-8 -*-

import your_module_name


__openerp__.py should contain: a description of your module, dependencies, vews etc

example:

{

'name': 'Base',

'version': '1.3',

'category': 'Hidden',

'description': """

The kernel of OpenERP, needed for all installation.

===================================================

""",

'author': 'OpenERP SA',

'maintainer': 'OpenERP SA',

'website': 'http://www.openerp.com',

'depends': [],

'data': [

'base_data.xml',

'res/res_currency_data.xml',

'res/res_country_data.xml',

'security/base_security.xml',

'module/module_report.xml',

'module/wizard/base_module_update_view.xml',

'res/res_company_view.xml',

'res/res_security.xml',

'security/ir.model.access.csv',

],

'demo': [

'base_demo.xml',

'res/res_partner_demo.xml',

'res/res_partner_demo.yml',

'res/res_partner_image_demo.xml',

],

'test': [

'tests/base_test.yml',

'tests/test_osv_expression.yml',

'tests/test_ir_rule.yml', # <-- These tests modify/add/delete ir_rules.

],

'installable': True,

'auto_install': True,

}

your_module_view.xml should contain your_module_view_definition:

example:

<?xml version="1.0" encoding="utf-8"?>

<openerp>

<data>

<record model="ir.actions.act_window" id="base.action_res_groups">

<field name="name">Groups</field>

<field name="context">{'search_default_no_share': 1}</field>

</record>

<record id="view_groups_form_share" model="ir.ui.view">

<field name="name">res.groups.form</field>

<field name="model">res.groups</field>

<field name="inherit_id" ref="base.view_groups_form"/>

<field name="arch" type="xml">

<field name="users" position="attributes">

<attribute name="context">{'search_default_no_share':1}</attribute>

</field>

<field name="name" position="after">

<field name="share"/>

</field>

</field>

</record>

</data>

</openerp>

your_module.py should contain:

#class with _columns will create your table with your columns

class your_class(osv.osv):

_name = 'your.class'#your new table

_description = 'Your class description'

_columns = {

'your_field_1': fields.type_of_field('Field name'),

'your_field_2': fields.type_of_field('Field name'),

'your_field_3': fields.type_of_field('Field name'),

'your_field_4': fields.type_of_field('Field name'),

}

    def your_def(self, cr, uid, args, fields, context=None):#check manual in case you need some special args o kwargs etc

        do something

Of course it's just a basics but .... that's all you need to build your own module. :)


Thank you so much Obx.... I appreciate your help.

Anchal
on 3/28/16, 5:41 AM

Nice answer @Dr. Obx +1

Ankit H Gandhi(AHG)
on 3/29/16, 7:01 AM

Your'e welcome :)

Dr Obx
on 3/31/16, 8:16 AM

While Dr Obx's answer is pretty thorough, might I point out: 1) it's missing the import statement in your_module.py 2) We should be helping new people learn the new API

Michael Watchorn
on 3/31/16, 12:51 PM

@Anchal: The key to creating a new table is the line: '_name = your.class'.

Michael Watchorn
on 3/31/16, 12:57 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 3/24/16, 4:19 AM
Seen: 861 times
Last updated: 3/31/16, 8:16 AM