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

How to generate values when click a button?

By
DM
on 2/13/15, 4:08 AM 1,016 views

Hello,

Im trying to create a button with method when you click on it would create a new row with default values in the tree view maybe there is any examples that i could have a look?

I already got the button just strugling with the function.

Any examples and help would be appreaciated

Thank you,

Why not using the Add Item link? Make the tree view not readonly?

Ivan
on 2/13/15, 4:28 AM

I mean yes add a link is a good example, but what im trying to do is generate lots of date when you click a button with predefined default values.

DM
on 2/13/15, 4:45 AM

Check the create_period method in odoo/odoo/addons/account/account.py. It is called from a button in view_account_fiscalyear_form view (odoo/addons/account/account_view.xml)

Ivan
on 2/13/15, 5:15 AM

And if you create without passing any value, it will use all default values.

Ivan
on 2/13/15, 5:16 AM

Ivan thanks very good example!

DM
on 2/13/15, 6:28 AM
1

Ludo - Neobis

--Ludo - Neobis--
2387
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--

Developer

Ludo - Neobis
On 2/13/15, 5:14 AM

If you simply want to create a record for any table (on API v7) you only need to call the create method and supply the required fields. For example, if you are on the view for sale orders and for some season you need to create a product, you could use following code:

product_obj = self.pool.get('product.product')

product_id = product_obj.create(cr, uid, {'name': 'My product'})

In this case, product only needs a name to be supplied in the function, but any other object needs to have a dictionary of required fields to be supplied. Of course this can also be used in a loop if you want to generate multiple entries. Something like so:

product_obj = self.pool.get('product.product')

names_list = ['item1', 'item2', 'item3']

for name in names_list:

    product_id = product_obj.create(cr, uid, {'name': name})

This is under the assumption that you use the ORM on Odoo v7. If you use the new API, creation is only slightly different.

1
DM
On 2/13/15, 6:27 AM

Thanks guys really helpful!


 

0

Vasanth

--Vasanth--
3458
| 5 3 8
Pondicherry, India
--Vasanth--
Vasanth
On 2/13/15, 5:48 AM

Try this :

You add this In py:

    _columns = {

        'id_no' : fields.char('id no', size=64),
}

    _defaults = {
        'id_no': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'sequence_code'),
}

    def add(self, cr, uid, ids, context):
        vals = {}
        vals['id_no'] = 'Pre'+ids[0]
        self.create(cr, SUPERUSER_ID, vals, context)
        return True

 

In view:

<record>
----------
 <field name="id_no"/>
<button name="add" colspan="1" string="Add" type="object" />
</record>

<record id="seq_unique_id" model="ir.sequence.type">
<field name="name">my_sequence</field>
<field name="code">sequence_code</field>
</record>

<record id="seq_unique_id2" model="ir.sequence">
<field name="name">my_sequence</field>
<field name="code">sequence_code</field>
<field name="prefix">prefix</field>
<field name="padding">3</field>
</record>

0

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 2/13/15, 5:15 AM

If you're looking for an example, there is one at page "Accounting/Configuration/Periods/Fiscal Years" when you create new fiscal year, there is two buttons "Create Monthly Periods" and "Create 3 Months Periods" with exactly same behavior as you're truing to implement. So you can take look and you'll get idea how to continue.

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: 2/13/15, 4:08 AM
Seen: 1016 times
Last updated: 10/23/15, 9:18 AM