Odoo状态切换模块报错求助:点击“设置为开始”触发api.py错误
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.multifor older versions,@api.modelor@api.actionfor newer ones like 14+). - Ensure the method name matches exactly what you're referencing in your view, and it actually updates the
statefield. 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
nameattribute of the button must match the method name in your model (case-sensitive!). - Set
type="object"(notaction) since you're calling a model method directly. - Ensure the
statesattribute restricts the button to the correct initial status (e.g., only show when state isdraft). 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
statefield:<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.csvfile to ensure the user's group has write access to your model. - If you've added custom groups, verify the button includes the correct
groupsattribute (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

