This question has been flagged
4 Replies
93 Vizualizări

I'm trying to limit options shown in the Packages window.  I have two Many2One fields:

  • Carrier
  • Package Type

When I select a certain "Carrier" I only want to see the package types associated with that carrier.  For example "dhl" has (pack1, pack2, pack3), "fedex" has (pack4, pack5, pack6) and "usps" has (pack7, pack8, pack9).


I have figured out how to do this with a single carrier in the Domain of the "Package Type" field:

[["package_carrier_type","=","dhl"]]

However, I haven't figured out how to properly write the domain to work with all the carrier options.  Any help is greatly appreciated.

V16

Avatar
Abandonează
Best Answer

Use this domain: [ '|',('carrier_id', '=', selected_carrier_id), ('carrier_id', '=', False)]

Avatar
Abandonează
Autor

Thank you for the input. Unfortunately, 'carrier_id' does not appear to be a valid domain entry in the Package Type field. Copying this code results in an "Domain not properly formed" error and I cannot find the 'carrier_id' when trying to add this manually in Studio with the Domain GUI.

Best Answer

Hi Brandon,

To achieve dynamic filtering of the "Package Type" field based on the selected "Carrier" in Odoo v16, you can use a computed domain in your model definition. Here's how you can set it up:

Ensure that the Carrier model has a relational field in the Package Type model and apply a domain for the package_type Many2one field using Studio.

You can define the domain directly in the Studio interface like this:

[('carrier_id', '=', carrier_id)]

This domain will filter the available options in the "Package Type" field based on the selected "Carrier"


Hope it helps

Avatar
Abandonează
Autor

Thank you for the input! I haven't been able to get this to work yet, but I don't think I fully understand how to "Ensure that the Carrier model has a relational field in the Package Type model", could you provide a bit more detail on hot to accomplish this?

Autor Best Answer

I have still been unable to get this to work.  Any help is greatly appreciated.

Avatar
Abandonează
Best Answer

Hi,

I'd be glad to help you with limiting package type options based on the selected carrier in Odoo V16. Here's the domain you can use for the "Package Type" field:

[ '|',('carrier_id', '=', selected_carrier_id), ('carrier_id', '=', False)]


Explanation:

  • [('carrier_id', '=', selected_carrier_id)]: This part filters the "Package Type" records where the carrier_id field is equal to the currently selected carrier's ID (selected_carrier_id). This ensures only package types associated with the chosen carrier are shown.
  • '|' (OR operator): This separates the two conditions within the domain.
  • ('carrier_id', '=', False)]: This part allows you to see package types that are not associated with any carrier (if applicable in your data model). This provides flexibility if you have some generic package types that can be used with any carrier.

Hope help you.

Avatar
Abandonează
Autor

Thanks for the info and explanation. Unfortunately, this doesn't seem to be working, I get the following error:
Unknown field "stock.package.type.carrier_id" in domain of <field name="package_type_id"> ([["carrier_id","=","selected_carrier_id"]]))

The "carrier_id" field appears to be a list of the Shipping Methods, but the fields available while creating the domain under the "package_carrier_type" are the base carriers selected as the "Provider" (delivery_type) within the Shipping method. I've tried using this modified code, but it's not working either (doesn't error, but the selection field stays empty):
[["package_carrier_type","=","selected_delivery_type"]]