Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
1229 มุมมอง
Hello, how could I create a button that creates a new record in another model containing data from my current model? from my python module


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

HI,
Utilize odoo create orm method:  Odoo Create ORM Method
Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด
This method will create a new record in the target model and populate its fields with data from the current model.

from odoo import models, fields

class YourModel(models.Model):
_name = 'your.model'
_description = 'Your Model'

name = fields.Char(string='Name')
description = fields.Text(string='Description')
related_field = fields.Many2one('other.model', string='Related Model')

def action_create_new_record(self):
for record in self:
# Create a new record in the other model
self.env['other.model'].create({
'name': record.name, # Pass data from the current model
'description': record.description,
'related_field': record.id,
})



Add a button to your form view in the XML file. Use the type="object" attribute to call a Python method when the button is clicked.


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

    <field name="name">your.model.form</field>

    <field name="model">your.model</field>

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

        <form string="Your Model">

            <header>

                <button name="action_create_new_record" type="object" string="Create Record" class="oe_highlight"/>

            </header>

            <sheet>

                <!-- Your form fields go here -->

            </sheet>

        </form>

    </field>

</record>



When you click the "Create Record" button on the form view of your current model, a new record will be created in the target model using the data from the current record.


Best Regards,
NIZAMUDHEEN MJ
Accurates

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
0
ต.ค. 19
4376
Can't import a model in odoo view แก้ไขแล้ว
7
ก.ค. 19
41908
3
พ.ค. 19
10253
2
ส.ค. 18
11738
1
มี.ค. 15
4238