How to create a Duplicate button.

Dale E. Moore

Settings, Companies, Companies, Bank Accounts does not have a duplicate button.

How would I create a push button that would duplicate a Bank Account like exists for Customers, Products, Projects, and so many other objects in OpenERP?

I've heard you "put a button on list view of o2m bank and call copy()!" Because "copy() does it all (default values + new values + translations)!" So this goes in my view.xml:

<record id="view_partner_bank_form_inherit_dale" model="ir.ui.view">
    <field name="name">Dale Bank Accounts - Journal</field>
    <field name="model"></field>
    <field name="type">form</field>
    <field name="inherit_id" ref="base.view_partner_bank_form"/>
    <field name="arch" type="xml">
    <group name="bank" position="after">
    <group name="accounting" col="2" colspan="2" attrs="{'invisible': [('company_id','=', False)]}" groups="base.group_extended">
        <separator string="Duplicate" colspan="2"/>
        <button name="copy_bank" type="object" string="Duplicate"/>

The button size is a bit large and I hear that "col, colspan; will adjust the size" then this goes in my

class bank(osv.osv):
    _inherit = ""

    def copy_bank(self, cr, uid, ids, context=None):
        for rec in self.browse(cr, uid, ids, context):
            self.copy(cr,uid, , {},context)
        return True

and everything works lovely.

I'd just like to move the Duplicate button up onto the same line with the Close button.

1 Comment
Naresh Soni (nch)

Hi Dale, you should not use the reserved keyword "copy" as a name of your method. Secondly it will be nice if you can post the python code that you have written for this method.

1 Answer
Parth Gajjar
Best Answer

Duplicate Button Generate Automatically

Duplicate Button will generate automatically in every form.

image description

You just need to override copy function if you want some changes in default copy function.

def copy(self, cr, uid, id, default=None, context=None):
    #your changes
    default_return_value=super("your parent class", self).copy(cr, uid, id, default, context)
    #your changes in "default_return_value"
    return default_return_value
1 Comment
Mathieu Stumpf

I'm doing just that to prevent a constraint which doesn't allow two objects with the same name to exist for my current class. But when I try to duplicate, the constraint error rise anyway. Should I overload something in the form view to make the button call my overloaded _copy_ method?