mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #10739: Introduce get_queryset() method on generic views
This commit is contained in:
committed by
Jeremy Stretch
parent
9628dead07
commit
b2e2e3be35
@ -82,23 +82,25 @@ class ThingEditView(ObjectEditView):
|
|||||||
Below are the class definitions for NetBox's object views. These views handle CRUD actions for individual objects. The view, add/edit, and delete views each inherit from `BaseObjectView`, which is not intended to be used directly.
|
Below are the class definitions for NetBox's object views. These views handle CRUD actions for individual objects. The view, add/edit, and delete views each inherit from `BaseObjectView`, which is not intended to be used directly.
|
||||||
|
|
||||||
::: netbox.views.generic.base.BaseObjectView
|
::: netbox.views.generic.base.BaseObjectView
|
||||||
|
options:
|
||||||
|
members:
|
||||||
|
- get_queryset
|
||||||
|
- get_object
|
||||||
|
- get_extra_context
|
||||||
|
|
||||||
::: netbox.views.generic.ObjectView
|
::: netbox.views.generic.ObjectView
|
||||||
options:
|
options:
|
||||||
members:
|
members:
|
||||||
- get_object
|
|
||||||
- get_template_name
|
- get_template_name
|
||||||
|
|
||||||
::: netbox.views.generic.ObjectEditView
|
::: netbox.views.generic.ObjectEditView
|
||||||
options:
|
options:
|
||||||
members:
|
members:
|
||||||
- get_object
|
|
||||||
- alter_object
|
- alter_object
|
||||||
|
|
||||||
::: netbox.views.generic.ObjectDeleteView
|
::: netbox.views.generic.ObjectDeleteView
|
||||||
options:
|
options:
|
||||||
members:
|
members: false
|
||||||
- get_object
|
|
||||||
|
|
||||||
::: netbox.views.generic.ObjectChildrenView
|
::: netbox.views.generic.ObjectChildrenView
|
||||||
options:
|
options:
|
||||||
@ -111,6 +113,10 @@ Below are the class definitions for NetBox's object views. These views handle CR
|
|||||||
Below are the class definitions for NetBox's multi-object views. These views handle simultaneous actions for sets objects. The list, import, edit, and delete views each inherit from `BaseMultiObjectView`, which is not intended to be used directly.
|
Below are the class definitions for NetBox's multi-object views. These views handle simultaneous actions for sets objects. The list, import, edit, and delete views each inherit from `BaseMultiObjectView`, which is not intended to be used directly.
|
||||||
|
|
||||||
::: netbox.views.generic.base.BaseMultiObjectView
|
::: netbox.views.generic.base.BaseMultiObjectView
|
||||||
|
options:
|
||||||
|
members:
|
||||||
|
- get_queryset
|
||||||
|
- get_extra_context
|
||||||
|
|
||||||
::: netbox.views.generic.ObjectListView
|
::: netbox.views.generic.ObjectListView
|
||||||
options:
|
options:
|
||||||
|
@ -38,6 +38,7 @@ A new `PluginMenu` class has been introduced, which enables a plugin to inject a
|
|||||||
* [#9072](https://github.com/netbox-community/netbox/issues/9072) - Enable registration of tabbed plugin views for core NetBox models
|
* [#9072](https://github.com/netbox-community/netbox/issues/9072) - Enable registration of tabbed plugin views for core NetBox models
|
||||||
* [#9880](https://github.com/netbox-community/netbox/issues/9880) - Introduce `django_apps` plugin configuration parameter
|
* [#9880](https://github.com/netbox-community/netbox/issues/9880) - Introduce `django_apps` plugin configuration parameter
|
||||||
* [#10314](https://github.com/netbox-community/netbox/issues/10314) - Move `clone()` method from NetBoxModel to CloningMixin
|
* [#10314](https://github.com/netbox-community/netbox/issues/10314) - Move `clone()` method from NetBoxModel to CloningMixin
|
||||||
|
* [#10739](https://github.com/netbox-community/netbox/issues/10739) - Introduce `get_queryset()` method on generic views
|
||||||
|
|
||||||
### Other Changes
|
### Other Changes
|
||||||
|
|
||||||
|
@ -1,18 +1,40 @@
|
|||||||
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
|
|
||||||
from utilities.views import ObjectPermissionRequiredMixin
|
from utilities.views import ObjectPermissionRequiredMixin
|
||||||
|
|
||||||
|
|
||||||
class BaseObjectView(ObjectPermissionRequiredMixin, View):
|
class BaseView(ObjectPermissionRequiredMixin, View):
|
||||||
|
queryset = None
|
||||||
|
|
||||||
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
self.queryset = self.get_queryset(request)
|
||||||
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_queryset(self, request):
|
||||||
"""
|
"""
|
||||||
Base view class for reusable generic views.
|
Return the base queryset for the view. By default, this returns self.queryset.all().
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request: The current request
|
||||||
|
"""
|
||||||
|
if self.queryset is None:
|
||||||
|
raise ImproperlyConfigured(
|
||||||
|
f"{self.__class__.__name__} does not define a queryset. Set queryset on the class or "
|
||||||
|
f"override its get_queryset() method."
|
||||||
|
)
|
||||||
|
return self.queryset.all()
|
||||||
|
|
||||||
|
|
||||||
|
class BaseObjectView(BaseView):
|
||||||
|
"""
|
||||||
|
Base class for generic views which display or manipulate a single object.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
queryset: Django QuerySet from which the object(s) will be fetched
|
queryset: Django QuerySet from which the object(s) will be fetched
|
||||||
template_name: The name of the HTML template file to render
|
template_name: The name of the HTML template file to render
|
||||||
"""
|
"""
|
||||||
queryset = None
|
|
||||||
template_name = None
|
template_name = None
|
||||||
|
|
||||||
def get_object(self, **kwargs):
|
def get_object(self, **kwargs):
|
||||||
@ -35,16 +57,15 @@ class BaseObjectView(ObjectPermissionRequiredMixin, View):
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
class BaseMultiObjectView(ObjectPermissionRequiredMixin, View):
|
class BaseMultiObjectView(BaseView):
|
||||||
"""
|
"""
|
||||||
Base view class for reusable generic views.
|
Base class for generic views which display or manipulate multiple objects.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
queryset: Django QuerySet from which the object(s) will be fetched
|
queryset: Django QuerySet from which the object(s) will be fetched
|
||||||
table: The django-tables2 Table class used to render the objects list
|
table: The django-tables2 Table class used to render the objects list
|
||||||
template_name: The name of the HTML template file to render
|
template_name: The name of the HTML template file to render
|
||||||
"""
|
"""
|
||||||
queryset = None
|
|
||||||
table = None
|
table = None
|
||||||
template_name = None
|
template_name = None
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user