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

规则随机生效排查:如何调试ir.rule模型当前满足的规则?

Troubleshooting Odoo ir.rule Issues & Debugging Tips

Hey there, let's walk through your questions one by one—these are super common pain points when working with Odoo's record rules, and I've dug into these exact scenarios before.

Why Do Rules Seem to Randomly Work/Fail, or Stop Working the Next Day?

First, let's demystify the "randomness" and daily failures:

  • Date-dependent domains: If your rule uses hardcoded date logic like datetime.now() instead of Odoo's context-aware fields.Date.today() or env.context.get('date'), the domain will shift when the calendar rolls over. For example, a rule filtering "records created today" will break tomorrow because the date value changes.
  • Company/context switching: Odoo rules often tie to company_id or other context variables (like warehouse, branch). If a user switches companies or the context changes between sessions, rules that rely on these values will stop matching.
  • Rule activation status: It's easy to accidentally toggle a rule's active flag, or have a rule with date_start/date_end set that expires overnight.
  • Admin user exemption: By default, Odoo's superadmin users (or users in the "Administration/Access Rights" group) bypass all record rules. This is intentional—admins are meant to have full access unless you explicitly override this by removing the perm_read/perm_write full permissions from their group.

How to Debug Which Rules Are Currently Applied?

Here are two practical ways to check active rules:

  1. Developer Mode Rule Inspection: Enable developer mode, go to Settings > Technical > Security > Record Rules, and filter for the model you're troubleshooting. Check each rule's Domain field, Active status, and Group assignments. You can even test the domain directly in the model's search bar to see if it returns the expected records.
  2. Log Computed Domains: The most reliable way is to add logging directly to the ir.rule model's core methods, which brings us to your next question.

Where to Add Logs in ir.rule to See Applied Domains?

The key method to hook into is _compute_domain (in Odoo 12+; older versions use domain_get). This method calculates the final combined domain that Odoo applies to a model for the current user and operation mode (read/write/delete).

Here's a code snippet you can use to inherit the ir.rule model and add detailed logging:

from odoo import models
import logging

# Set up a logger specific to ir.rule debugging
_logger = logging.getLogger(__name__)

class IrRule(models.Model):
    _inherit = 'ir.rule'

    def _compute_domain(self, model_name, mode='read'):
        # Call the original method first to get the computed domain
        final_domain = super()._compute_domain(model_name, mode)
        
        # Log all relevant details
        _logger.info("\n=== ir.rule Debug Log ===")
        _logger.info(f"User: {self.env.user.name} (ID: {self.env.user.id})")
        _logger.info(f"Model: {model_name}, Operation Mode: {mode}")
        _logger.info(f"Final Applied Domain: {final_domain}")
        _logger.info(f"Current Context: {self.env.context}")
        
        # Log each individual rule's details (helpful for spotting inactive/non-applicable rules)
        for rule in self:
            _logger.info(f"Rule Name: {rule.name} | Active: {rule.active} | Domain: {rule.domain}")
        _logger.info("=========================\n")
        
        return final_domain

Why This Works:

  • _compute_domain runs every time Odoo checks record access, so you'll get a log entry for every read/write/delete operation.
  • You'll see exactly which rules are considered, their active status, and the final merged domain that's applied to the user's request.
  • The log includes context info, which is critical for catching issues like company switching or date context mismatches.

Additional Debugging Tips

  • Clear Odoo Cache: Sometimes rules get cached incorrectly. Restart your Odoo server or go to Settings > Technical > Actions > Clear Cache to refresh rule data.
  • Test with Non-Admin Users: Since admins bypass rules, always test with a regular user assigned to the group your rule targets.
  • Check Rule Priority: If multiple rules apply to the same model and group, their domains are combined with AND logic. A conflicting rule might be overriding the one you expect to work.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:04:03