mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Fix model URL generator for plugins
This commit is contained in:
parent
0b194e363e
commit
bb3b933115
@ -10,6 +10,7 @@ from mptt.models import MPTTModel, TreeForeignKey
|
|||||||
from netbox.models.features import *
|
from netbox.models.features import *
|
||||||
from utilities.mptt import TreeManager
|
from utilities.mptt import TreeManager
|
||||||
from utilities.querysets import RestrictedQuerySet
|
from utilities.querysets import RestrictedQuerySet
|
||||||
|
from utilities.views import get_viewname
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -42,7 +43,7 @@ class NetBoxFeatureSet(
|
|||||||
return f'{settings.STATIC_URL}docs/models/{self._meta.app_label}/{self._meta.model_name}/'
|
return f'{settings.STATIC_URL}docs/models/{self._meta.app_label}/{self._meta.model_name}/'
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse(f'{self._meta.app_label}:{self._meta.model_name}', args=[self.pk])
|
return reverse(get_viewname(self), args=[self.pk])
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
import taggit.managers
|
||||||
|
import utilities.json
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('dummy_plugin', '0001_initial'),
|
||||||
|
('extras', '0122_charfield_null_choices'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='dummymodel',
|
||||||
|
name='created',
|
||||||
|
field=models.DateTimeField(auto_now_add=True, null=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='dummymodel',
|
||||||
|
name='custom_field_data',
|
||||||
|
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='dummymodel',
|
||||||
|
name='last_updated',
|
||||||
|
field=models.DateTimeField(auto_now=True, null=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='dummymodel',
|
||||||
|
name='tags',
|
||||||
|
field=taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag'),
|
||||||
|
),
|
||||||
|
]
|
@ -1,7 +1,9 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from netbox.models import NetBoxModel
|
||||||
|
|
||||||
class DummyModel(models.Model):
|
|
||||||
|
class DummyModel(NetBoxModel):
|
||||||
name = models.CharField(
|
name = models.CharField(
|
||||||
max_length=20
|
max_length=20
|
||||||
)
|
)
|
||||||
|
@ -6,4 +6,5 @@ from . import views
|
|||||||
urlpatterns = (
|
urlpatterns = (
|
||||||
path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'),
|
path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'),
|
||||||
path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'),
|
path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'),
|
||||||
|
path('models/<int:pk>/', views.DummyModelView.as_view(), name='dummymodel'),
|
||||||
)
|
)
|
||||||
|
@ -5,6 +5,7 @@ from django.http import HttpResponse
|
|||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
|
|
||||||
from dcim.models import Site
|
from dcim.models import Site
|
||||||
|
from netbox.views import generic
|
||||||
from utilities.views import register_model_view
|
from utilities.views import register_model_view
|
||||||
from .models import DummyModel
|
from .models import DummyModel
|
||||||
# Trigger registration of custom column
|
# Trigger registration of custom column
|
||||||
@ -18,6 +19,10 @@ class DummyModelsView(View):
|
|||||||
return HttpResponse(f"Instances: {instance_count}")
|
return HttpResponse(f"Instances: {instance_count}")
|
||||||
|
|
||||||
|
|
||||||
|
class DummyModelView(generic.ObjectView):
|
||||||
|
queryset = DummyModel.objects.all()
|
||||||
|
|
||||||
|
|
||||||
class DummyModelAddView(View):
|
class DummyModelAddView(View):
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
|
13
netbox/netbox/tests/test_model.py
Normal file
13
netbox/netbox/tests/test_model.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from netbox.tests.dummy_plugin.models import DummyModel
|
||||||
|
|
||||||
|
|
||||||
|
class ModelTest(TestCase):
|
||||||
|
|
||||||
|
def test_absolute_url(self):
|
||||||
|
m = DummyModel(name='Foo')
|
||||||
|
m.full_clean()
|
||||||
|
m.save()
|
||||||
|
|
||||||
|
self.assertEqual(m.get_absolute_url(), f"/plugins/dummy-plugin/models/{m.pk}/")
|
Loading…
Reference in New Issue
Block a user