From bbfdcdce73fde992b5f5472fffabd764e2e5911e Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 20 Sep 2025 10:16:10 +1000 Subject: [PATCH] Support import of "choice" fields (#10361) - Perform reverse lookup of display value --- src/backend/InvenTree/importer/models.py | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/backend/InvenTree/importer/models.py b/src/backend/InvenTree/importer/models.py index 6a887785bd..6521414dec 100644 --- a/src/backend/InvenTree/importer/models.py +++ b/src/backend/InvenTree/importer/models.py @@ -628,6 +628,38 @@ class DataImportRow(models.Model): if value is None and field in default_values: value = default_values[field] + # If the field provides a set of valid 'choices', use that as a lookup + if field_type == 'choice' and 'choices' in field_def: + choices = field_def.get('choices', None) + + if callable(choices): + choices = choices() + + # Try to match the provided value against the available choices + choice_value = None + + for choice in choices: + primary_value = choice['value'] + display_value = choice['display_name'] + + if primary_value == value: + choice_value = primary_value + # Break on first match against a primary choice value + break + + if display_value == value: + choice_value = primary_value + + elif ( + str(display_value).lower().strip() == str(value).lower().strip() + and choice_value is None + ): + # Case-insensitive match against display value + choice_value = primary_value + + if choice_value is not None: + value = choice_value + data[field] = value self.data = data