소스 검색

Document YAML export support and deprecated ObjectListView export methods

Jeremy Stretch 3 일 전
부모
커밋
10f8aa36a5
2개의 변경된 파일과 27개의 추가작업 그리고 2개의 파일을 삭제
  1. 24 1
      docs/plugins/development/exporters.md
  2. 3 1
      docs/plugins/development/views.md

+ 24 - 1
docs/plugins/development/exporters.md

@@ -65,6 +65,29 @@ def ready(self):
 !!! warning "Exporters must be stateless"
 !!! warning "Exporters must be stateless"
     Registration instantiates the class once, and that single instance serves every request. Don't store per-request data on `self`.
     Registration instantiates the class once, and that single instance serves every request. Don't store per-request data on `self`.
 
 
+## YAML Export
+
+NetBox's YAML exporter supports any model which defines a `to_yaml()` method, including plugin models. The method returns a YAML document representing the object, and the exporter joins the documents for all exported objects.
+
+To avoid a database query per object, set `yaml_prefetch` on the model to the related objects which `to_yaml()` accesses through reverse or many-to-many relations. The exporter passes these to the queryset's `prefetch_related()`. Related objects accessed through foreign keys are fetched in batches automatically.
+
+```python title="models.py"
+import yaml
+
+from netbox.models import NetBoxModel
+
+class Widget(NetBoxModel):
+    ...
+    yaml_prefetch = ('parts',)
+
+    def to_yaml(self):
+        data = {
+            'name': self.name,
+            'parts': [part.name for part in self.parts.all()],
+        }
+        return yaml.dump(data, sort_keys=False)
+```
+
 ## Export Formats
 ## Export Formats
 
 
 The `export_format` query parameter selects an exporter by its slug:
 The `export_format` query parameter selects an exporter by its slug:
@@ -74,7 +97,7 @@ The `export_format` query parameter selects an exporter by its slug:
 | `?export=table&export_format=<slug>` | Current view, in the given format                      |
 | `?export=table&export_format=<slug>` | Current view, in the given format                      |
 | `?export&export_format=<slug>`       | All data, in the given format                          |
 | `?export&export_format=<slug>`       | All data, in the given format                          |
 | `?export=table`                      | Current view, as CSV                                   |
 | `?export=table`                      | Current view, as CSV                                   |
-| `?export`                            | All data, as YAML if the model supports it, or as CSV  |
+| `?export`                            | All data, as [YAML](#yaml-export) if the model supports it, or as CSV |
 
 
 An unknown slug, or an exporter that doesn't support the model (or the current view), returns a 404 response. Exporters are available only in the web UI. The REST API supports [export templates](../../customization/export-templates.md) only.
 An unknown slug, or an exporter that doesn't support the model (or the current view), returns a 404 response. Exporters are available only in the web UI. The REST API supports [export templates](../../customization/export-templates.md) only.
 
 

+ 3 - 1
docs/plugins/development/views.md

@@ -154,9 +154,11 @@ Below are the class definitions for NetBox's multi-object views. These views han
     options:
     options:
       members:
       members:
         - get_table
         - get_table
-        - export_table
         - export_template
         - export_template
 
 
+!!! warning "Deprecation Warning"
+    The `export_table()` and `export_yaml()` methods of `ObjectListView` have been deprecated beginning in NetBox v4.8, and are scheduled for removal in NetBox v5.0. An overridden `export_table()` applies only to CSV exports, and an overridden `export_yaml()` only to YAML exports. To provide a customized export format, register an [exporter](./exporters.md) instead. To prefetch related objects for YAML exports, set [`yaml_prefetch`](./exporters.md#yaml-export) on the model.
+
 ::: netbox.views.generic.BulkImportView
 ::: netbox.views.generic.BulkImportView
     options:
     options:
       members:
       members: