mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
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:
parent
53ac6c724d
commit
a23235400d
@ -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"""
|
||||
|
@ -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')
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user