mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Replace all instances of .extra() in QuerySets with annotations, including references in docstrings
This commit is contained in:
@ -6,7 +6,7 @@ class PrefixQuerySet(QuerySet):
|
|||||||
def annotate_depth(self, limit=None):
|
def annotate_depth(self, limit=None):
|
||||||
"""
|
"""
|
||||||
Iterate through a QuerySet of Prefixes and annotate the hierarchical level of each. While it would be preferable
|
Iterate through a QuerySet of Prefixes and annotate the hierarchical level of each. While it would be preferable
|
||||||
to do this using .extra() on the QuerySet to count the unique parents of each prefix, that approach introduces
|
to do this using .annotate() on the QuerySet to count the unique parents of each prefix, that approach introduces
|
||||||
performance issues at scale.
|
performance issues at scale.
|
||||||
|
|
||||||
Because we're adding a non-field attribute to the model, annotation must be made *after* any QuerySet
|
Because we're adding a non-field attribute to the model, annotation must be made *after* any QuerySet
|
||||||
|
@ -2,6 +2,7 @@ import netaddr
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||||
from django.db.models import Count, Q
|
from django.db.models import Count, Q
|
||||||
|
from django.db.models.expressions import RawSQL
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
from django_tables2 import RequestConfig
|
from django_tables2 import RequestConfig
|
||||||
@ -291,10 +292,10 @@ class RIRBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
|
|||||||
|
|
||||||
class AggregateListView(PermissionRequiredMixin, ObjectListView):
|
class AggregateListView(PermissionRequiredMixin, ObjectListView):
|
||||||
permission_required = 'ipam.view_aggregate'
|
permission_required = 'ipam.view_aggregate'
|
||||||
# TODO: Replace raw SQL
|
queryset = Aggregate.objects.prefetch_related('rir').annotate(
|
||||||
queryset = Aggregate.objects.prefetch_related('rir').extra(select={
|
child_count=RawSQL('SELECT COUNT(*) FROM ipam_prefix WHERE ipam_prefix.prefix <<= ipam_aggregate.prefix', ())
|
||||||
'child_count': 'SELECT COUNT(*) FROM ipam_prefix WHERE ipam_prefix.prefix <<= ipam_aggregate.prefix',
|
)
|
||||||
})
|
|
||||||
filter = filters.AggregateFilter
|
filter = filters.AggregateFilter
|
||||||
filter_form = forms.AggregateFilterForm
|
filter_form = forms.AggregateFilterForm
|
||||||
table = tables.AggregateDetailTable
|
table = tables.AggregateDetailTable
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from django.db.models import Manager
|
from django.db.models import Manager
|
||||||
|
from django.db.models.expressions import RawSQL
|
||||||
|
|
||||||
NAT1 = r"CAST(SUBSTRING({}.{} FROM '^(\d{{1,9}})') AS integer)"
|
NAT1 = r"CAST(SUBSTRING({}.{} FROM '^(\d{{1,9}})') AS integer)"
|
||||||
NAT2 = r"SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')"
|
NAT2 = r"SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')"
|
||||||
@ -21,11 +22,11 @@ class NaturalOrderingManager(Manager):
|
|||||||
db_field = self.natural_order_field
|
db_field = self.natural_order_field
|
||||||
|
|
||||||
# Append the three subfields derived from the designated natural ordering field
|
# Append the three subfields derived from the designated natural ordering field
|
||||||
queryset = queryset.extra(select={
|
queryset = (
|
||||||
'_nat1': NAT1.format(db_table, db_field),
|
queryset.annotate(_nat1=RawSQL(NAT1.format(db_table, db_field), ()))
|
||||||
'_nat2': NAT2.format(db_table, db_field),
|
.annotate(_nat2=RawSQL(NAT2.format(db_table, db_field), ()))
|
||||||
'_nat3': NAT3.format(db_table, db_field),
|
.annotate(_nat3=RawSQL(NAT3.format(db_table, db_field), ()))
|
||||||
})
|
)
|
||||||
|
|
||||||
# Replace any instance of the designated natural ordering field with its three subfields
|
# Replace any instance of the designated natural ordering field with its three subfields
|
||||||
ordering = []
|
ordering = []
|
||||||
|
Reference in New Issue
Block a user