mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-15 12:08:17 -06:00
Add Custom Fields to the CSV export template
This commit is contained in:
parent
9cb6b792ba
commit
585f61d38c
@ -94,15 +94,30 @@ class ObjectListView(View):
|
|||||||
Export the queryset of objects as comma-separated value (CSV), using the model's to_csv() method.
|
Export the queryset of objects as comma-separated value (CSV), using the model's to_csv() method.
|
||||||
"""
|
"""
|
||||||
csv_data = []
|
csv_data = []
|
||||||
|
custom_fields = []
|
||||||
|
|
||||||
# Start with the column headers
|
# Start with the column headers
|
||||||
headers = ','.join(self.queryset.model.csv_headers)
|
headers = ','.join(self.queryset.model.csv_headers)
|
||||||
|
|
||||||
|
if hasattr(self.queryset.model, 'get_custom_fields') and len(self.queryset) > 0:
|
||||||
|
for item in self.queryset[0].get_custom_fields():
|
||||||
|
headers += ',' + 'cf_{}'.format(str(item.name))
|
||||||
|
custom_fields.append(item.name)
|
||||||
|
|
||||||
csv_data.append(headers)
|
csv_data.append(headers)
|
||||||
|
|
||||||
# Iterate through the queryset appending each object
|
# Iterate through the queryset appending each object
|
||||||
for obj in self.queryset:
|
for obj in self.queryset:
|
||||||
data = csv_format(obj.to_csv())
|
data = obj.to_csv()
|
||||||
csv_data.append(data)
|
field_data = obj.cf()
|
||||||
|
|
||||||
|
for field in custom_fields:
|
||||||
|
if field in field_data:
|
||||||
|
data += (field_data[field],)
|
||||||
|
else:
|
||||||
|
data += ('',)
|
||||||
|
|
||||||
|
csv_data.append(csv_format(data))
|
||||||
|
|
||||||
return csv_data
|
return csv_data
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user