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.

0

[SOLVED] How to generate my own create button in a form?

By
Juan Formoso
on 10/27/14, 12:40 PM 6,344 views

I have created a button of type action. This button calls an action, and this action calls a form. This form was totally created by me, and has a button Create, which calls the function create, directly inherited from orm.Model (I did not touch it).

When I click on Create, the record is perfectly stored on the database, but then I get this error:

File "/opt/openerp7/openerp_70_ocb_devel/ocb-server/openerp/osv/orm.py", line 4342, in create
    if vals.pop(f, None) is not None:
TypeError: pop() takes at most 1 argument (2 given)

I was looking for the content of vals, and when I click on the button, the method create seems to be called twice. The first time, vals brings the values of the record in a dictionary (all right), the second time brings the ID of the new record generated in a list, and provokes the error.

I would like to know why is this happening and how to solve this. I left here my code:

The button which calls the action:

<button name="%(res_partner_extended.action_create_res_partner_link_category)d" string="Link to category" type="action"/>

The action:

<record id="action_create_res_partner_link_category" model="ir.actions.act_window">
    <field name="name">Link to a new category</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">res.partner.link.category</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="view_id" ref="res_partner_link_category_create_form_view"/>
    <field name="target">new</field>
</record>

And the form, with the button which is getting the error:

<record id="res_partner_link_category_create_form_view" model="ir.ui.view">
    <field name="name">res.partner.link.category.create.form</field>
    <field name="model">res.partner.link.category</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Linked category" version="7.0">
            <group>
                <group col="2">
                    <field name="partner_id" readonly="1" options="{'no_open': True,}"/>
                    <field name="link_category_id" required="1" options="{'no_open': True}"/>
                    <field name="type" required="1"/>
                </group>
                <group col="2">
                    <field name="date" required="1"/>
                    <field name="observations"/>
                </group>
            </group>
            <footer>
                <button name="create" string="Create" type="object" class="oe_highlight"/>
                or
                <button string="Discard" class="oe_link" special="cancel" />
            </footer>
        </form>
    </field>
</record>

Can you post your create function?

Pau Ubach
on 10/27/14, 9:34 PM

@Pau Ubach I did not made any create function. I am calling the create function which every class inherited from orm.Model has.

Juan Formoso
on 10/28/14, 4:14 AM

Maybe I'm suggesting something stupid, in case the method should be called twice, but I'd try to make CREATE function, check the content in vals, if OK, call create, otherwise, don't call it.

Pau Ubach
on 10/28/14, 4:21 AM

Thank you @Pau Ubach. That's not stupid, in fact, I tried it before, creating my own create function and from there calling the ORM create method. But the result was exactly the same. I do not know what I am doing wrong. I am thinking to ignore ORM method and make a SQL query directly, but that would not be a right way to manage my target.

Juan Formoso
on 10/28/14, 4:30 AM

Neither using a SQL nor overriding the CREATE function to act this way aren't good solutions, as this could affect the normal system behavior. I'm sorry to can't be more helpful. You should wait until it comes someone with better background in openERP than me.

Pau Ubach
on 10/28/14, 4:34 AM
0
Juan Formoso
On 10/28/14, 10:57 AM

Finally, I got the solution after seeing the behaviour of the button.

When I clicked the button, first, the record was stored in the database (not because of the method I wrote in name, but because other reason I do not know). After that, the method I wrote in the name of the button was executed, receiving a list with the new ID of the new generated record instead of the classic dictionary vals (with its fields and their values), so that sparked an error.

So finally I decided to change the method of the name, as I had done before, and I wrote create_rplc instead of create. The important point is inside this function, where I removed the line self.create(cr, uid, vals). This way the content of the function now is only return {'type': 'ir.actions.act_window_close'}.

The conclusion: it is not necessary to tell the button to create the record, it is something that it does automatically, but it is mandatory to specify a method with the property name, and inside this method do not create any record.

I do not why the reason. If someone knows, it would be great if he enlightened us. Thank you!

I have seen this before: when you click a button in a form, for example when changing state, the record is automatically saved. I'd say it's the normal behavior in OpenERP when a button is clicked.

Pau Ubach
on 10/28/14, 9:07 PM
1
Ajay
On 10/28/14, 7:19 AM

Hello Juan,

From previous conversation i came to know that you have "res.partner.link.category" you customized object & on the values from that object you need to create 'res.partner' record. Make me correct if i m wrong.

Now if you have the same scenario which i declare above i have one question your 'res.partner.link.category' object is osv.osv or osv.memory ?

In you case you need to change the method name like 'create_category' & in that method you can call create method of res.partner object with passing the values you want.

If I understood well, my case is the other way round. I want to create a record of res.partner.link.category from the res.partner form. The button which begins the action is in res.partner form, and it calls the popup (a form of res.partner.link.category). Inside this popup is where the create button is... res.partner.link.category inherits from orm.Model (not from orm.TransientModel).

Juan Formoso
on 10/28/14, 7:31 AM

If 'res.partner.link.category' is not osv.memory you can change target of action to current like new . It will automatically gives you button to create record. May be this will help you.

Ajay
on 10/28/14, 8:25 AM

@Ajay you are right, I turned new into current and I was able to create records without errors (but I needed the popup). Finally, I got the solution. I am going to post it.

Juan Formoso
on 10/28/14, 10:41 AM
0

Ludo - Neobis

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

Developer

Ludo - Neobis
On 10/28/14, 6:09 AM

I am not quite sure what you are trying to achieve here, but it sounds like a complicated work chain and not something OpenERP is suitable for. As Pau Ubach already pointed out, it is rarely a good idea to surpass the ORM and use queries instead. You will get into all sort of trouble later (especially with objects that have a workflow).

Could you please describe what the end goal is? I assume it has something to do with partner categories when creating partners, but other than that I fail to see what the requirements are.

My target is to generate a create button in the res.partner form, which will create a record from other different model, res.partner.link.category. They are not related at all. But I have to include that button inside res.partner form.

Juan Formoso
on 10/28/14, 6:13 AM

Ok, makes more sense now. So, if you made a regular button "type=action name=create_partner_link_method" and in create_partner_link_method on the res_partner object you would call the linked objects create ( i.e. : 'self.pool.get(' res.partner.link.category').create(cr,uid,my_link_vals) )function, that would give you an error? Note that you should not call the method that the button itself calls "create" because that is already in the override.

Ludo - Neobis
on 10/28/14, 6:19 AM

What's the relationship between these 2 objects?

Pau Ubach
on 10/28/14, 6:27 AM

Aren't you including a field to the second object in the form?

Pau Ubach
on 10/28/14, 6:29 AM

The problem is that I cannot declare the function "create_partner_link_method" in the "res.partner" object, because the form in which the create button is, belongs to res.partner.link.category model (otherwise, I could not show the fields of that model in the form, because they weren't recognized). But if I change the name of the method (create_partner_link_method), as you say, and in the definition of this method (in res.partner.link.category model) I write self.create(cr, uid, my_link_vals), the result is the same.

Juan Formoso
on 10/28/14, 6:35 AM

@Pau Ubach Yes, I do. In fact, models are related, I do not have to allow creating records as a normal one2many/many2many form, I have to generate a button to create records (out of posssibles trees), so it is like they weren't related.

Juan Formoso
on 10/28/14, 6:39 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

3 follower(s)

Stats

Asked: 10/27/14, 12:40 PM
Seen: 6344 times
Last updated: 8/23/15, 12:44 PM