Просмотр исходного кода

Closes #23742: Deprecate object access in export templates

Mark the queryset variable and the model classes available to export
templates as deprecated in v4.8.0, for removal in v5.0. Config
templates are not affected. The plugin docs now note that
get_jinja_context() also feeds export templates.
Jason Novinger 2 дней назад
Родитель
Сommit
170c67c858

+ 3 - 0
docs/customization/export-templates.md

@@ -22,6 +22,9 @@ Height: {{ rack.u_height }}U
 {% endfor %}
 ```
 
+!!! warning "Deprecation Warning"
+    The `queryset` variable and the model classes available in export templates (for example, `dcim.Device`) have been deprecated as of NetBox v4.8.0, and are scheduled for removal in NetBox v5.0. Export templates that use them will need to be updated for NetBox v5.0. Config templates are not affected.
+
 To access custom fields of an object within a template, use the `cf` attribute. For example, `{{ obj.cf.color }}` will return the value (if any) for a custom field named `color` on `obj`.
 
 If you need to use the config context data in an export template, you'll should use the function `get_config_context` to get all the config context data. For example:

+ 3 - 0
docs/models/extras/exporttemplate.md

@@ -20,6 +20,9 @@ Template code may optionally be sourced from a remote [data file](../core/datafi
 
 Jinja2 template code for rendering the exported data.
 
+!!! warning "Deprecation Warning"
+    Use of `queryset` and model classes in template code has been deprecated as of NetBox v4.8.0, and is scheduled for removal in NetBox v5.0. See the [export templates documentation](../../customization/export-templates.md) for details.
+
 ### Environment Parameters
 
 A dictionary of any additional parameters to pass when instantiating the [Jinja2 environment](https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment). Jinja2 supports various optional parameters which can be used to modify its default behavior.

+ 4 - 4
docs/plugins/development/config-templates.md

@@ -5,7 +5,7 @@
 NetBox uses [Jinja](https://jinja.palletsprojects.com/) to render [configuration templates](../../features/configuration-rendering.md). Plugins can extend this rendering pipeline in two complementary ways:
 
 1. **Register custom filters** — make new template filters available by name in every config template.
-2. **Inject context variables** — add extra variables that are available inside every config template render.
+2. **Inject context variables** — add extra variables that are available inside every config template and export template render.
 
 ---
 
@@ -83,7 +83,7 @@ JINJA_FILTERS = {
 
 ## Injecting Context Variables
 
-Override `get_jinja_context()` in your `PluginConfig` subclass to inject additional variables into every config template render context.
+Override `get_jinja_context()` in your `PluginConfig` subclass to inject additional variables into every config template and export template render context.
 
 ```python title="my_plugin/__init__.py"
 from netbox.plugins import PluginConfig
@@ -99,14 +99,14 @@ class MyPluginConfig(PluginConfig):
         }
 ```
 
-The returned dict is merged into the template context, so `my_plugin` becomes available by name inside every config template:
+The returned dict is merged into the template context, so `my_plugin` becomes available by name inside every config template and export template:
 
 ```jinja2
 {% set records = my_plugin.lookup(device.name) %}
 ```
 
 !!! warning "Startup cost"
-    `get_jinja_context()` is called on **every** config template render, not once at startup. Keep it fast. Defer expensive lookups to the object you return rather than performing them in `get_jinja_context()` itself.
+    `get_jinja_context()` is called on **every** config template and export template render, not once at startup. Keep it fast. Defer expensive lookups to the object you return rather than performing them in `get_jinja_context()` itself.
 
 !!! note "Conflict avoidance"
     Choose context variable names that are unlikely to collide with NetBox's built-in template variables (`device`, `queryset`, etc.) or with those contributed by other plugins. Prefixing with your plugin name is strongly recommended.

+ 1 - 0
netbox/extras/models/mixins.py

@@ -144,6 +144,7 @@ class RenderTemplateMixin(models.Model):
         _context = defaultdict(dict)
 
         # Populate all public models for reference within the template
+        # TODO: Remove in NetBox v5.0 for export templates only (config templates keep them)
         for object_type in ObjectType.objects.public():
             if model := object_type.model_class():
                 _context[object_type.app_label][model.__name__] = model

+ 1 - 0
netbox/extras/models/models.py

@@ -577,6 +577,7 @@ class ExportTemplate(
         # Fetch each related object the template reads for the whole queryset at once
         if queryset is not None:
             queryset = queryset.fetch_mode(FETCH_PEERS)
+        # TODO: Remove in NetBox v5.0
         _context['queryset'] = queryset
         return _context