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

Odoo 10 Fleet模块:如何设置Conductor字段双向唯一性约束?

Solution for Bidirectional Uniqueness in Fleet Driver Assignment

Got it, let's tackle this bidirectional uniqueness requirement for the Conductor (driver) field in Odoo 10's Fleet module. Here's how to make it work properly:

First, let's break down what we need to enforce:

  • A vehicle can only have one driver (this is already handled by the driver_id being a Many2one field, since it links to a single partner record by default)
  • A driver cannot be assigned to multiple vehicles (this is the key constraint we need to add)

Below are two reliable implementation methods, plus troubleshooting tips for why your previous code might have failed:

SQL constraints are enforced directly at the database level, making them faster and more robust against concurrent operations.

  1. In your custom module, create or edit the model file that inherits fleet.vehicle (e.g., models/fleet_vehicle.py):
from odoo import models, fields, api

class FleetVehicle(models.Model):
    _inherit = 'fleet.vehicle'

    # Add SQL constraint to ensure driver_id is unique across all vehicle records
    _sql_constraints = [
        ('unique_driver_assignment', 'UNIQUE(driver_id)', 'A driver cannot be assigned to more than one vehicle!'),
    ]
  • Note: SQL UNIQUE constraints allow multiple NULL values, so vehicles without a driver will still be permitted (which is usually the desired behavior).
  1. Deploy the changes:
    • Restart your Odoo server (required for production environments)
    • Upgrade your custom module via the Apps menu (or run odoo-bin -u your_module_name in the command line for development mode)

Method 2: Python Constraint (For Custom Logic Flexibility)

If you need to add extra custom checks (like excluding certain driver types), use a Python constraint instead:

from odoo import models, fields, api
from odoo.exceptions import ValidationError

class FleetVehicle(models.Model):
    _inherit = 'fleet.vehicle'

    @api.constrains('driver_id')
    def _check_unique_driver(self):
        # Skip check if no driver is assigned
        if not self.driver_id:
            return
        # Search for existing vehicles linked to this driver (exclude current record)
        existing_vehicles = self.search([
            ('driver_id', '=', self.driver_id.id),
            ('id', '!=', self.id)
        ])
        if existing_vehicles:
            raise ValidationError(f"This driver is already assigned to vehicle {existing_vehicles[0].license_plate}!")

Troubleshooting Why Your Previous Code Didn't Work

If your earlier implementation failed, check these common pitfalls:

  • Incorrect Model Inheritance: Ensure you're properly inheriting the base fleet.vehicle model with _inherit = 'fleet.vehicle' (don't create a new model from scratch).
  • Module Not Upgraded: After modifying code, you must upgrade your custom module—Odoo won't pick up changes automatically.
  • Syntax Errors: Look for indentation issues, missing imports (like ValidationError), or typos in field names (e.g., driver instead of driver_id).
  • Constraint Name Conflict: Make sure your SQL constraint name is unique (Odoo will throw an error if you reuse a name from the base Fleet module).
  • Ignoring NULL Values: If you used a Python constraint, ensure you skip checks when driver_id is empty to avoid false positive errors.

Once you implement either method and deploy correctly, trying to assign a driver to multiple vehicles will trigger a clear error message, enforcing the bidirectional uniqueness you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:26:49