You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo状态切换模块报错求助:点击“设置为开始”触发api.py错误

Troubleshooting Odoo Status Switch Failure

Hey there! Let's work through this status toggle issue in your Odoo module. The error you're hitting in api.py (method = getattr(type(model), name)) is a clear sign that Odoo can't locate the method you're trying to trigger when clicking "设置为开始". Let's break down the most likely issues in your statusbar_model.py and statusbar_view.xml files, along with fixes:

1. Verify the Status Method in Your Model

First, double-check that your status switch method is properly defined in statusbar_model.py:

  • Make sure the method uses the correct Odoo API decorator (depending on your Odoo version: @api.multi for older versions, @api.model or @api.action for newer ones like 14+).
  • Ensure the method name matches exactly what you're referencing in your view, and it actually updates the state field. Example:
    from odoo import models, fields, api
    
    class YourModel(models.Model):
        _name = 'your.model.name'
        _description = 'Your Model'
    
        state = fields.Selection([
            ('draft', 'Draft'),
            ('start', 'Started'),
            # Add other status options here
        ], string='Status', default='draft')
    
        @api.multi
        def set_to_start(self):
            # Make sure you're writing the correct state value
            self.write({'state': 'start'})
    

2. Check View Button Configuration

In statusbar_view.xml, confirm your button is correctly linked to the model method:

  • The name attribute of the button must match the method name in your model (case-sensitive!).
  • Set type="object" (not action) since you're calling a model method directly.
  • Ensure the states attribute restricts the button to the correct initial status (e.g., only show when state is draft). Example:
    <button name="set_to_start" string="设置为开始" type="object" states="draft" class="oe_highlight"/>
    
  • Also, make sure your statusbar widget is bound to the correct state field:
    <field name="state" widget="statusbar" statusbar_visible="draft,start"/>
    

3. Rule Out Permission Issues

Odoo might block the method call if the current user doesn't have the right permissions:

  • Check your ir.model.access.csv file to ensure the user's group has write access to your model.
  • If you've added custom groups, verify the button includes the correct groups attribute (e.g., groups="base.group_user").

4. Fix Cache/Module Upgrade Problems

Sometimes code changes don't take effect because of cached data or an un-upgraded module:

  • Enable Developer Mode, then go to Apps > Find your module > Click "Upgrade".
  • If that doesn't work, restart your Odoo server to clear any cached class definitions.

5. Confirm State Field Definition

Double-check that your state field is a Selection type (not a regular char field) and that the status values you're using match between the model and view. Mismatched values (e.g., using 'started' in the method but 'start' in the selection) will break the switch.

If you've gone through all these steps and still have issues, feel free to share snippets of your actual statusbar_model.py and statusbar_view.xml code—we can dig deeper!

内容的提问来源于stack exchange,提问作者ayar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:00:27