1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

CSV import/export custom fields

This commit is contained in:
Saria Hajjar
2020-01-10 14:26:39 +00:00
parent 789cf827f2
commit f1d5e28f13
8 changed files with 51 additions and 35 deletions

View File

@@ -78,15 +78,28 @@ class ObjectListView(View):
Export the queryset of objects as comma-separated value (CSV), using the model's to_csv() method.
"""
csv_data = []
custom_fields = []
# Start with the column headers
headers = ','.join(self.queryset.model.csv_headers)
# Add custom field headers
content_type = ContentType.objects.get_for_model(self.queryset.model)
for custom_field in CustomField.objects.filter(obj_type=content_type):
headers += ',cf_{}'.format(custom_field.name)
custom_fields.append(custom_field.name)
csv_data.append(headers)
# Iterate through the queryset appending each object
for obj in self.queryset:
data = csv_format(obj.to_csv())
csv_data.append(data)
data = obj.to_csv()
for custom_field in custom_fields:
data += (obj.cf.get(custom_field, ''),)
csv_data.append(csv_format(data))
return csv_data