Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
5810 Tampilan

Hi,

I'm inheriting the Sale Order view and adding a button in it. What I'm trying to achieve is calling a method in my .py model but Odoo is looking for it in the sale.order one. 

This is my .py model: https://pastebin.com/aiKGLMdE


This is the viewhttps://pastebin.com/ypn75Nvd


This is the errorhttps://pastebin.com/LEwtc9aw


Any help is appreciated, thanks in advance
 (PS: I'm usign Odoo.sh)

Avatar
Buang
Jawaban Terbai

Hi,

Please go through the Odoo documentation:

https://www.odoo.com/documentation/15.0/developer/reference/backend/orm.html#classical-inheritance

You are creating a new class through Classical inheritance(Prototype inheritance). You can't inherit the existing view in this case(You are supposed to use the new class instead of sale.order). So, create a new view for your new model.

Example

If you check the fields in 'sale.order', you can see these fields:

transaction_ids = fields.Many2many('payment.transaction',
'sale_order_transaction_rel',
'sale_order_id', 'transaction_id',
string='Transactions', copy=False,
readonly=True)
authorized_transaction_ids = fields.Many2many(
string="Authorized Transactions", comodel_name='payment.transaction',
compute='_compute_authorized_transaction_ids', readonly=True, copy=False)

The authorized_transaction_ids is a computed field which depends on the transaction_ids field. So, changing the field will cause another error. The tag_ids field is also there.

During development, if you are not intending to update any field, then please use a different name for the new fields to avoid unnecessary errors and to not break existing workflows!

Now, you are supposed to add 3 fields in 'sale.order' and a button in its form view. I have refactored the code here(please make the changes as per your requirement). I think you are adding the customization through the module "custom_addon"(based on the path in the error message.)

custom_addon/models/sale_order.py

from odoo import fields, models

class SaleOrder(models.Model):
_inherit = 'sale.order'

field_1 = fields.Char(string="Field One", required=True)
sale_order_transaction_ids = fields.Many2many(
'sale.order.transaction', 'sale_order_transaction_sale_order_rel',
'sale_order_id', 'sale_order_transaction_id',
string='Sale Order Transactions')
sale_order_tag_ids = fields.Many2many(
'sale.order.tag', 'sale_order_tag_sale_order_rel', 'sale_order_id',
'sale_order_tag_id', string='Sale Order Tags')

def action_test_function(self):
print("Test Print")
return

custom_addon/views/sale_order_views.xml

<?xml version="1.0" encoding="utf-8"?>


<odoo>


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


        <field name="name">sale.order.view.form.inherit.custom_addon</field>


        <field name="model">sale.order</field>


        <field name="inherit_id" ref="sale.view_order_form"/>


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


            <xpath expr="/form/sheet/group/group[1]" position="inside">


                <button name="action_test_function" string="Test" type="object"


                        class="btn-primary"/>


            </xpath>


        </field>


    </record>


</odoo>


Ensure that you have added the python files and directories in the respective __init__.py files, added the xml files in the __manifest__.py, and added "sale_management" module in the depends list.

Regards

Avatar
Buang
Penulis

i'm trying to add a new button linked to a new function in a existing view, according to documentation I should avoid "_name" parameter if I want to add a new element or function, but still not works

Penulis

any help?

Hi,
We have updated the answer, please check

Penulis

i struggled for days to look for a solution, and this finally worked, thank you so much! have a great day!

Post Terkait Replies Tampilan Aktivitas
1
Sep 15
2985
2
Jun 23
2358
1
Jun 23
2172
1
Nov 21
2745
2
Mei 21
5984