2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-10-03 15:52:51 +00:00

Support import of "choice" fields (#10361)

- Perform reverse lookup of display value
This commit is contained in:
Oliver
2025-09-20 10:16:10 +10:00
committed by GitHub
parent c9e74c5910
commit bbfdcdce73

View File

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