diff --git a/netbox/dcim/migrations/0170_configtemplate.py b/netbox/dcim/migrations/0170_configtemplate.py index b1aac0ad20..f9508424d4 100644 --- a/netbox/dcim/migrations/0170_configtemplate.py +++ b/netbox/dcim/migrations/0170_configtemplate.py @@ -13,7 +13,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='device', name='config_template', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='devices', to='extras.configtemplate'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='%(class)ss', to='extras.configtemplate'), ), migrations.AddField( model_name='devicerole', diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 6b8e927436..857251caf4 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -24,7 +24,7 @@ from utilities.fields import ColorField, CounterCacheField, NaturalOrderingField from utilities.tracking import TrackingModelMixin from .device_components import * -from .mixins import WeightMixin +from .mixins import RenderConfigMixin, WeightMixin __all__ = ( @@ -525,7 +525,14 @@ def update_interface_bridges(device, interface_templates, module=None): interface.save() -class Device(ContactsMixin, ImageAttachmentsMixin, PrimaryModel, ConfigContextModel, TrackingModelMixin): +class Device( + ContactsMixin, + ImageAttachmentsMixin, + RenderConfigMixin, + ConfigContextModel, + TrackingModelMixin, + PrimaryModel +): """ A Device represents a piece of physical hardware mounted within a Rack. Each Device is assigned a DeviceType, DeviceRole, and (optionally) a Platform. Device names are not required, however if one is set it must be unique. @@ -686,13 +693,6 @@ class Device(ContactsMixin, ImageAttachmentsMixin, PrimaryModel, ConfigContextMo validators=[MaxValueValidator(255)], help_text=_('Virtual chassis master election priority') ) - config_template = models.ForeignKey( - to='extras.ConfigTemplate', - on_delete=models.PROTECT, - related_name='devices', - blank=True, - null=True - ) latitude = models.DecimalField( verbose_name=_('latitude'), max_digits=8, @@ -1070,17 +1070,6 @@ def primary_ip(self): def interfaces_count(self): return self.vc_interfaces().count() - def get_config_template(self): - """ - Return the appropriate ConfigTemplate (if any) for this Device. - """ - if self.config_template: - return self.config_template - if self.role.config_template: - return self.role.config_template - if self.platform and self.platform.config_template: - return self.platform.config_template - def get_vc_master(self): """ If this Device is a VirtualChassis member, return the VC master. Otherwise, return None. diff --git a/netbox/dcim/models/mixins.py b/netbox/dcim/models/mixins.py index f787c8e97b..95f6d41fe9 100644 --- a/netbox/dcim/models/mixins.py +++ b/netbox/dcim/models/mixins.py @@ -4,6 +4,11 @@ from dcim.choices import * from utilities.utils import to_grams +__all__ = ( + 'RenderConfigMixin', + 'WeightMixin', +) + class WeightMixin(models.Model): weight = models.DecimalField( @@ -44,3 +49,27 @@ def clean(self): # Validate weight and weight_unit if self.weight and not self.weight_unit: raise ValidationError(_("Must specify a unit when setting a weight")) + + +class RenderConfigMixin(models.Model): + config_template = models.ForeignKey( + to='extras.ConfigTemplate', + on_delete=models.PROTECT, + related_name='%(class)ss', + blank=True, + null=True + ) + + class Meta: + abstract = True + + def get_config_template(self): + """ + Return the appropriate ConfigTemplate (if any) for this Device. + """ + if self.config_template: + return self.config_template + if self.role.config_template: + return self.role.config_template + if self.platform and self.platform.config_template: + return self.platform.config_template diff --git a/netbox/templates/virtualization/virtualmachine.html b/netbox/templates/virtualization/virtualmachine.html index 04e038b92d..27f5ea1149 100644 --- a/netbox/templates/virtualization/virtualmachine.html +++ b/netbox/templates/virtualization/virtualmachine.html @@ -43,6 +43,10 @@
{% trans "Config Template" %} | +{{ config_template|linkify|placeholder }} | +
---|---|
{% trans "Data Source" %} | +{{ config_template.data_file.source|linkify|placeholder }} | +
{% trans "Data File" %} | +{{ config_template.data_file|linkify|placeholder }} | +
{{ context_data|pprint }}+
{{ rendered_config }}+ {% else %} +