Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate adding a custom tabbed view to a core model (v3.4) #23

Open
jeremystretch opened this issue Feb 3, 2023 · 2 comments
Open

Comments

@jeremystretch
Copy link
Member

NetBox v3.4 introduced the ability for plugins to register custom views that appear as tabs on core model views (FR #9072).

@Bapths
Copy link

Bapths commented Apr 19, 2023

Hi!

I am trying to use this feature but I can't get it to work. What I want to do is adding a tab on tenants that lists (with a table I created) some related objects.

I tried something like this (in myplugin/views.py) inspired by netbox plugin documentation but it surely isn't the solution:

from tenancy.models import Tenant
from netbox.views import generic
from . import models, tables
from utilities.views import ViewTab, register_model_view

@register_model_view(Tenant, name='myview', path='some-other-stuff')
class MyView(generic.ObjectListView):
    queryset = models.PluginObject.objects.all()
    table = tables.PluginObjectTable
    tab = ViewTab(
        label='Other Stuff',
        badge=lambda obj: obj.stuff_related_name.count(),
        permission='myplugin.view_stuff'
    )

The tab does render with the correct count badge but I get an ObjectListView.get() got an unexpected keyword argument 'pk' error when trying to load the tab view.

Thanks to anybody that could help me 😄


EDIT: I finally got this to work by looking into the netbox code / other plugins code.

Here is my view:

@register_model_view(Tenant, name='myview', path='some-other-stuff')
class MyView(generic.ObjectChildrenView):
    queryset = Tenant.objects.all().prefetch_related('stuff_set')
    child_model = models.Stuff
    table = tables.StuffTable
    template_name = "myplugin/stuffchild.html"
    hide_if_empty = True
    tab = ViewTab(
        label='My Custom View',
        badge=lambda obj: obj.stuff_set.count(),
        permission='myplugin.view_stuff'
    )

    def get_children(self, request, parent):
        return parent.stuff_set.all()

and I added the following in template/myplugin/stuffchild.html:

{% extends 'generic/object.html' %}
{% load render_table from django_tables2 %}

{% block content %}
    <div class="card">
        <div class="card-body" id="object_list">
            {% render_table table %}
        </div>
    </div>
{% endblock content%}

I don't know if this is the right way to do but it works fine for me 😄

@mytlogos
Copy link

Had the same issue, but wanted to comment, that one can use the generic template generic/object_children.html of netbox, if one just wants a list without any further content/buttons etc.
This would replace the custom template of @Bapths .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants