mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-07 07:48:50 +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_ROWS = 1000
|
||||||
MAX_IMPORT_COLS = 100
|
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(
|
def import_data_inner(
|
||||||
self,
|
self,
|
||||||
dataset,
|
dataset,
|
||||||
@ -79,6 +82,13 @@ class InvenTreeResource(ModelResource):
|
|||||||
|
|
||||||
return [f for f in fields if f.column_name not in fields_to_exclude]
|
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):
|
class CustomRateAdmin(RateAdmin):
|
||||||
"""Admin interface for the Rate class"""
|
"""Admin interface for the Rate class"""
|
||||||
|
@ -356,9 +356,32 @@ class BomItemAdmin(ImportExportModelAdmin):
|
|||||||
autocomplete_fields = ('part', 'sub_part',)
|
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):
|
class ParameterTemplateAdmin(ImportExportModelAdmin):
|
||||||
"""Admin class for the PartParameterTemplate model"""
|
"""Admin class for the PartParameterTemplate model"""
|
||||||
|
|
||||||
|
resource_class = ParameterTemplateResource
|
||||||
|
|
||||||
list_display = ('name', 'units')
|
list_display = ('name', 'units')
|
||||||
|
|
||||||
search_fields = ('name', 'units')
|
search_fields = ('name', 'units')
|
||||||
|
@ -3346,7 +3346,10 @@ class PartParameterTemplate(MetadataMixin, models.Model):
|
|||||||
})
|
})
|
||||||
|
|
||||||
# Check that 'choices' are in fact valid
|
# 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:
|
if self.choices:
|
||||||
choice_set = set()
|
choice_set = set()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user