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.

2

How to open a new form when i click in a button?

By
Arunagiri
on 10/25/14, 7:51 AM 8,249 views

I want to create button field . If i click a button it will show a new form . Please tell anyone how to create it  

Here is my code

myform.py

import time
from lxml import etree
import openerp.addons.decimal_precision as dp
import openerp.exceptions

from openerp import netsvc
from openerp import pooler
from openerp.osv import fields, osv, orm
from openerp.tools.translate import _

 

class my_form(osv.osv):
            _name = "my.form"
            _description = 'Formview Module'
            _columns = {
               'name': fields.char('Name', size=64),
               'project': fields.char('Project', size=64),
               'image': fields.binary('Image'),
               'file':fields.binary('attach file')
 #~ def create_field(self, cr, uid, ids, context=None):              
}
my_form()

class my_form2(osv.osv):
        _name="my.form2"
        _description="new form"
        _columns={
          'add':fields.char('Address',size=64)
          }
my_form2()

myform_view.xml

<openerp>
    <data>
        <record model="ir.ui.view" id="from_view_form">
        <field name="name">form.view.form</field>
        <field name="model">my.form</field>
        <field name="arch" type="xml">
            <form string="Form" version="7.0">
                <group>
                    <field name="name" />
                    <field name="project" />
                    <button name="%(create_field)d" string="Create Field" type="action" />
                </group>
            </form>
        </field>
    </record>
        <record model="ir.ui.view" id="from_view_form2">
        <field name="name">form.view.form</field>
        <field name="model">my.form</field>
        <field name="arch" type="xml">
            <form string="Form" version="7.0">
               <group>
                   <field name="name"/>
               </group>
            </form>
        </field>
    </record>

         <record model="ir.ui.view" id="from_view_tree">
        <field name="name">form.view.tree</field>
        <field name="model">my.form</field>
        <field name="arch" type="xml">
            <tree string="Form">
                <field name="name" />
            </tree>
        </field>
    </record>    
        
        <record model='ir.actions.act_window' id='%(create_field)d'>
        <field name="name">Form</field>
        <field name="res_model">my.form</field>
        <field name="view_type">form</field>
        <field name="view_mode">form</field>
        <field name="context">{}</field>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">
                Click to create a new record.
            </p>
            <p>This is a test class developed to learn Openerp.</p>
        </field>
    </record>
    
    
        <record model='ir.actions.act_window' id='form_view_action'>
        <field name="name">Form</field>
        <field name="res_model">my.form</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{}</field>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">
                Click to create a new record.
            </p>
            <p>This is a test class developed to learn Openerp.</p>
        </field>
    </record>

         <menuitem id="myform_ID" name="myforms" />

         <menuitem id="myform_menu_ID" name="myform" parent="myform_ID"  />

         <menuitem id="myform_menu2_ID" name="myform" parent="myform_menu_ID"  action='form_view_action' />

        
        
        
    </data>
    
    </openerp>

 

 

import time from lxml import etree import openerp.addons.decimal_precision as dp import openerp.exceptions from openerp import netsvc from openerp import pooler from openerp.osv import fields, osv, orm from openerp.tools.translate import _ class my_form(osv.osv): _name = "my.form" _description = 'Formview Module' _columns = { 'name': fields.char('Name', size=64), 'project': fields.char('Project', size=64), 'image': fields.binary('Image'), 'file':fields.binary('attach file') #~ def create_field(self, cr, uid, ids, context=None): } my_form() class my_form2(osv.osv): _name="my.form2" _description="new form" _columns={ 'add':fields.char('Address',size=64) } my_form2()

Arunagiri
on 10/27/14, 1:37 AM

i try myself but its not working please tell me ef create_field(self,cr, uid, ids,create_field, context={}): if not ids: return False if context is None: context={} model_data_pool = self.pool.get('ir.model.data') view = model_data_pool.get_object(cr, uid, 'my_form2', 'view_copy_multiple', context=context) return{ 'name': _("Copy multiple records"), 'type': 'ir.actions.act_window', 'view_mode': 'form', 'view_id': [view.id], 'view_type': 'form', 'res_model': 'my.form', 'nodestroy': True, 'target': 'new', 'context': context, 'res_id': ids, }

Arunagiri
on 10/29/14, 7:59 AM
3
Indrabhan Bhamare
On 7/21/15, 9:11 AM

Hello May be this will help you.

You can define object type button and return action.

example.py and example.xml

    @api.multi
def method_name(self):
view_id = self.env.ref('modul_name._id_of_form_view').id
context = self._context.copy()
return {
'name':'form_name',
'view_type':'form',
'view_mode':'tree',
'views' : [(view_id,'form')],
'res_model':'model_name',
'view_id':view_id,
'type':'ir.actions.act_window',
'res_id':self.id,
'target':'new',
'context':context,
}

<button name="method_name" string="open_form" type="object"/> 
3
Bailey
On 7/21/15, 6:48 AM

Hi

Try like this ...

<button type="action" string="submit" name= "%(module_name.form_action_id)d" class="oe_highlight"/>

which form u want open,put ur id mentioned in  above line( form_action-id),,, <record model="ir.ui.view" id="......."


2

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 10/29/14, 2:26 AM

Hi,

<button name="%(create_field)d" string="Create Field" type="action" />

Button with type "action" is like above will call the action with ID="create_field". So, you have to make one action with ID = "create_field".

But you have created action with ID = "%(create_field)d" is not good. please change it with ID = "create_field"

I hope this will help you. If you have still any issue then let me know.

Or 

You can put the button type="object" and make one function into python with same name as button name.

in your case :

<button name="create_field" string="Create Field" type="object" />

In your python code put the function as like below.

def create_field(self, cr, uid, ids, context=None):

        return {
            'name': _('My Form'),
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'my.form',
            'view_id': False,
            'type': 'ir.actions.act_window',
        }

It will open one window to create record of the model 'my.form'. If you want to open window to create 'my.form2' then just replate 'res_model' value with 'my.form2'.

thats it.

 

I already try that one but its not working . Any changes in function for action of button in python file if you know please tell me

Arunagiri
on 10/29/14, 2:38 AM

Then you have to take button type="object" and then return the form view from that button click function. When you take type="object" then system will call the function of python as same name of that button.

Emipro Technologies Pvt. Ltd.
on 10/29/14, 2:50 AM

Hi i change type ="object" but i got an error like this AttributeError: 'my.form' object has no attribute 'create_field'

Arunagiri
on 10/29/14, 7:32 AM

Thats the thing i asked how to write a function i don't know i'm new to openerp if you tell about that it will helpfull to me . This is my test project only i just want to learn please help me

Arunagiri
on 10/29/14, 7:47 AM

Hello, I have update my answer please have a look on it.

Emipro Technologies Pvt. Ltd.
on 10/29/14, 7:55 AM

def create_field(self,cr, uid, ids,create_field, context={}): if not ids: return False if context is None: context={} model_data_pool = self.pool.get('ir.model.data') view = model_data_pool.get_object(cr, uid, 'my_form2', 'view_copy_multiple', context=context) return{ 'name': _("Copy multiple records"), 'type': 'ir.actions.act_window', 'view_mode': 'form', 'view_id': [view.id], 'view_type': 'form', 'res_model': 'my.form', 'nodestroy': True, 'target': 'new', 'context': context, 'res_id': ids, }

Arunagiri
on 10/29/14, 8:00 AM

Hi i follow your code instructions i'm working odoo 8 i had an error again here is the error message : AccessError The requested operation cannot be completed due to security restrictions. Please contact your system administrator. (Document type: ir.actions.actions, Operation: read)

Arunagiri
on 10/30/14, 6:18 AM

For that you have to make sure that user have an access rights for the model ir.actions.actions. Thats it. you can also give access rights to a particular model via setting => Technical => Security => Access control List.

Emipro Technologies Pvt. Ltd.
on 10/30/14, 6:33 AM

Hi i change the Access control list till i got an error please give me any suggestion

Arunagiri
on 11/1/14, 3:35 AM
0
Chandni
On 10/15/16, 2:21 AM

Hi,

Refer this site, it might be help u.

http://www.odooclass.com/shop/product/how-to-create-custom-smart-buttons-in-odoo-8-24


Creating Smart Buttons in Odoo

1. Add required fields to the model so you can store your calculations

voucher_count = fields.Integer(compute='_voucher_count', string='# of Vouchers')

account_voucher_ids = fields.One2many('account.voucher', 'partner_id', 'Voucher History')

2. Create a custom view template and use the interhit id that matches the external id of the form 

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

<field name="name">view.res.partner.form.crm.inherited2</field> <field name="model">res.partner</field> <field name="priority" eval="200"/> <field name="inherit_id" ref="base.view_partner_form"/>

3. Use xpath in the template to tell the Odoo framework where to place your button

<xpath expr="//div[@name='buttons']" position="inside">

4. Add your Odoo smart button code to the view

<button class="oe_inline oe_stat_button" type="action" name="%(pay_view_voucher_tree)d" attrs="{'invisible': [('customer', '=', False)]}" icon="fa-usd"> <field string="Payments" name="voucher_count" widget="statinfo"/> </button>

5. Create a python function to calcuate the functional field (voucher_count)

@api.one @api.depends('account_voucher_ids','account_voucher_ids.state')

def _voucher_count(self):

        vouchers = self.env['account.voucher'].search([('partner_id', '=', self.id)])

        self.voucher_count = len(vouchers)


6. . Create a view for the action to display your results (payments in this case) when the user clicks the button

<record model="ir.actions.act_window" id="pay_view_voucher_tree">

<field name="name">Payments</field>

<field name="res_model">account.voucher</field>

<field name="view_type">form</field>

<field name="view_mode">tree,form,graph</field>

<field name="context">{'search_default_partner_id': active_id}</field>

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

          <tree string="Payments">

               <field name="date"/> <field name="number"/> <field name="reference"/> <field name="partner_id"/>

                <field name="type" invisible="context.get('visible', True)"/> <field name="amount" sum="Total Amount"/> <field name="state"/>

         </tree>

  </field>

</record>

If you found this as correct, please mark this as correct. Thanks.

0
kirubanidhi
On 7/7/16, 9:04 AM

In odoo 9 , 

We apply for this way


<button string="Library details" type="object"

class="oe_highlight oe_right" name="library_method" icon="fa-fire" >

</button>

@api.multi

def library_method(self):

print self._context

course_form = self.env.ref('college.return_form', False)

return {

'name': 'New Course',

'type': 'ir.actions.act_window',

'res_model': 'college.return',

'view_type': 'form',

'view_mode': 'tree,form',

'target': 'self',

'views': [(course_form.id, 'form')],

'view_id': 'course_form.id',

'flags': {'action_buttons': True},

}

0

I agree with Arunagiri, that is the way to resolve your problem and, in the action dict definition, you push also the 'target' property to show modal dialog

You know about the module if you let me know please ................!!!!

Arunagiri
on 11/1/14, 11:43 PM
0
Jacky
On 10/27/14, 1:27 AM

Question is not clear. Give an example. I think you can make it through a wizard.

i add a button in my module i want to new form when i click a button

Arunagiri
on 10/27/14, 1:37 AM

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

1 follower(s)

Stats

Asked: 10/25/14, 7:51 AM
Seen: 8249 times
Last updated: 10/15/16, 2:21 AM