From d12c335032128f5cafc7a390e04e0fe04773b760 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 20 Sep 2025 12:56:53 +1000 Subject: [PATCH] Support import of "choice" fields (#10361) (#10364) - Perform reverse lookup of display value (cherry picked from commit bbfdcdce73fde992b5f5472fffabd764e2e5911e) Co-authored-by: Oliver --- 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 468fe11542..0c5a61b56f 100644 --- a/src/backend/InvenTree/importer/models.py +++ b/src/backend/InvenTree/importer/models.py @@ -630,6 +630,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