diff --git a/InvenTree/InvenTree/admin.py b/InvenTree/InvenTree/admin.py index a9b68ece4f..12d6820db1 100644 --- a/InvenTree/InvenTree/admin.py +++ b/InvenTree/InvenTree/admin.py @@ -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""" diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index bb0946892e..c41622c5c6 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -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') diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index b3b409b5b1..be9fa33170 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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()