xixo222
xixo222

Reputation: 225

Odoo 14 "my_method_name" is not valid action on "my_module_name"

I am learning odoo 14 and I am trying to add a button in my form view. Unfortunately, every time I try to upgrade my custom module I get this error:

odoo.exceptions.ValidationError: Error while validating view:

button_custom_method is not a valid action on library.book

My custom module python file library_book.py:

from odoo import models, fields, api

class LibraryBook(models.Model):
    _name = 'library.book'
    _description = 'Library Book'

    name = fields.Char('Title', required=True)
    date_release = fields.Date('Release Date')
    author_ids = fields.Many2many('res.partner', string='Authors')
    
    def button_custom_method(self):
        print("Button custom text")

And my view library_book.xml:

<odoo>
    <!-- Form View -->
    <record id="library_book_view_form" model="ir.ui.view">
        <field name="name">Library Book Form</field>
        <field name="model">library.book</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="button_custom_method" string="Please click me" type="object"/>
                </header>
                <group>
                    <group>
                        <field name="name"/>
                        <field name="author_ids" widget="many2many_tags"/>
                    </group>
                    <group>
                        <field name="date_release"/>
                    </group>
                </group>
            </form>
        </field>
    </record>
</odoo>

Upvotes: 1

Views: 3615

Answers (4)

Kenly
Kenly

Reputation: 26708

Odoo will show this error message when there is no function with the same name as your button on the view model where you defined the button.

elif type_ == 'object':
  func = getattr(type(name_manager.Model), name, None)
  if not func:
      msg = _(
          "%(action_name)s is not a valid action on %(model_name)s",
          action_name=name, model_name=name_manager.Model._name,
      )
      self.handle_view_error(msg)

Probably, you defined the function on library.book model and you forgot to restart Odoo.

Upvotes: 1

Wald Sin
Wald Sin

Reputation: 208

I had the same issue right now. I didn`t solve it for half an hour, and then I realized, that I put button method in wrong model)

Upvotes: 0

Put type="object" first:

<button type="object" name="button_custom_method" string="Please click me"/>

Upvotes: -3

BunsGlazing
BunsGlazing

Reputation: 41

I Had the same issue than I realized that I had forgotten to import the file in the "__init__.py" file.

Upvotes: 4

Related Questions