This question has been flagged
20 Replies
72713 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>

 

 

Avatar
Discard
Author

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()

Author

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, }

Best Answer

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"/> 
Avatar
Discard
Best Answer

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="......."


Avatar
Discard
Best Answer

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.

 

Avatar
Discard
Author

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

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.

Author

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

Author

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

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

Author

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, }

Author

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)

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.

Author

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

Best Answer

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.

Avatar
Discard

Your answer is not relevant to this question! He simply want a button which will open a form view. Say for example our sale order line set as editable top. And we want to put a new button on sale order line which will popup sale order line form view in which we can edit things.

Best Answer

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},

}

Avatar
Discard

hey how to visible a tab while clicking a button , the condition where 2 boolean field is true

Create a new wizard form. 

Check the existing code by using 



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,
}



On Tue, 2 Nov, 2021, 6:08 pm ANSU MARY JACOB, <ansu@zbeanztech.com> wrote:

hey how to visible a tab while clicking a button , the condition where 2 boolean field is true

Sent by Odoo S.A. using Odoo.

Best Answer

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

Avatar
Discard
Author

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

Best Answer

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

Avatar
Discard
Author

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