Improve data import for PartParameterTemplate (#6182)

- Create Resource class which uses InvenTreeResource base
- Ensure 'units' field is converted to string if empty
- Handle null choices field in PartParameterTemplate model
This commit is contained in:
Oliver 2024-01-10 22:41:52 +11:00 committed by GitHub
parent 53ac6c724d
commit a23235400d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -19,6 +19,9 @@ class InvenTreeResource(ModelResource):
MAX_IMPORT_ROWS = 1000
MAX_IMPORT_COLS = 100
# List of fields which should be converted to empty strings if they are null
CONVERT_NULL_FIELDS = []
def import_data_inner(
self,
dataset,
@ -79,6 +82,13 @@ class InvenTreeResource(ModelResource):
return [f for f in fields if f.column_name not in fields_to_exclude]
def before_import_row(self, row, row_number=None, **kwargs):
"""Run custom code before importing each row"""
for field in self.CONVERT_NULL_FIELDS:
if field in row and row[field] is None:
row[field] = ''
class CustomRateAdmin(RateAdmin):
"""Admin interface for the Rate class"""

View File

@ -356,9 +356,32 @@ class BomItemAdmin(ImportExportModelAdmin):
autocomplete_fields = ('part', 'sub_part',)
class ParameterTemplateResource(InvenTreeResource):
"""Class for managing ParameterTemplate import/export"""
# The following fields will be converted from None to ''
CONVERT_NULL_FIELDS = [
'choices',
'units'
]
class Meta:
"""Metaclass definition"""
model = models.PartParameterTemplate
skip_unchanged = True
report_skipped = False
clean_model_instances = True
exclude = [
'metadata',
]
class ParameterTemplateAdmin(ImportExportModelAdmin):
"""Admin class for the PartParameterTemplate model"""
resource_class = ParameterTemplateResource
list_display = ('name', 'units')
search_fields = ('name', 'units')

View File

@ -3346,7 +3346,10 @@ class PartParameterTemplate(MetadataMixin, models.Model):
})
# Check that 'choices' are in fact valid
self.choices = self.choices.strip()
if self.choices is None:
self.choices = ''
else:
self.choices = str(self.choices).strip()
if self.choices:
choice_set = set()