2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-11-13 11:26:42 +00:00

Support import of "choice" fields (#10361) (#10364)

- Perform reverse lookup of display value

(cherry picked from commit bbfdcdce73)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
github-actions[bot]
2025-09-20 12:56:53 +10:00
committed by GitHub
parent eae580deaf
commit d12c335032

View File

@@ -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