diff --git a/netbox/ipam/migrations/0026_prefix_ordering_vrf_nulls_first.py b/netbox/ipam/migrations/0026_prefix_ordering_vrf_nulls_first.py new file mode 100644 index 000000000..17b169b24 --- /dev/null +++ b/netbox/ipam/migrations/0026_prefix_ordering_vrf_nulls_first.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2 on 2019-04-20 00:57 + +from django.db import migrations +import django.db.models.expressions + + +class Migration(migrations.Migration): + + dependencies = [ + ('ipam', '0025_custom_tag_models'), + ] + + operations = [ + migrations.AlterModelOptions( + name='prefix', + options={'ordering': [django.db.models.expressions.OrderBy(django.db.models.expressions.F('vrf'), nulls_first=True), 'family', 'prefix'], 'verbose_name_plural': 'prefixes'}, + ), + ] diff --git a/netbox/ipam/models.py b/netbox/ipam/models.py index 8ffb6f51b..35287dc52 100644 --- a/netbox/ipam/models.py +++ b/netbox/ipam/models.py @@ -4,7 +4,7 @@ from django.contrib.contenttypes.fields import GenericRelation from django.core.exceptions import ValidationError, ObjectDoesNotExist from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models -from django.db.models import Q +from django.db.models import F, Q from django.db.models.expressions import RawSQL from django.urls import reverse from taggit.managers import TaggableManager @@ -332,7 +332,7 @@ class Prefix(ChangeLoggedModel, CustomFieldModel): ] class Meta: - ordering = ['vrf', 'family', 'prefix'] + ordering = [F('vrf').asc(nulls_first=True), 'family', 'prefix'] verbose_name_plural = 'prefixes' def __str__(self): diff --git a/netbox/ipam/querysets.py b/netbox/ipam/querysets.py index bfb2525f2..9fd9bb6c1 100644 --- a/netbox/ipam/querysets.py +++ b/netbox/ipam/querysets.py @@ -1,7 +1,7 @@ -from utilities.sql import NullsFirstQuerySet +from django.db.models import QuerySet -class PrefixQuerySet(NullsFirstQuerySet): +class PrefixQuerySet(QuerySet): def annotate_depth(self, limit=None): """ diff --git a/netbox/utilities/sql.py b/netbox/utilities/sql.py deleted file mode 100644 index d76bc339e..000000000 --- a/netbox/utilities/sql.py +++ /dev/null @@ -1,32 +0,0 @@ -from django.db import connections, models -from django.db.models.sql.compiler import SQLCompiler - - -class NullsFirstSQLCompiler(SQLCompiler): - - def get_order_by(self): - result = super().get_order_by() - if result: - return [(expr, (sql + ' NULLS FIRST', params, is_ref)) for (expr, (sql, params, is_ref)) in result] - return result - - -class NullsFirstQuery(models.sql.query.Query): - - def get_compiler(self, using=None, connection=None): - if using is None and connection is None: - raise ValueError("Need either using or connection") - if using: - connection = connections[using] - return NullsFirstSQLCompiler(self, connection, using) - - -class NullsFirstQuerySet(models.QuerySet): - """ - Override PostgreSQL's default behavior of ordering NULLs last. This is needed e.g. to order Prefixes in the global - table before those assigned to a VRF. - """ - - def __init__(self, model=None, query=None, using=None, hints=None): - super().__init__(model, query, using, hints) - self.query = query or NullsFirstQuery(self.model)