Просмотр исходного кода

Closes #3094: Remove NullsFirstQuerySet

Jeremy Stretch 6 лет назад
Родитель
Сommit
e4c06700bb

+ 18 - 0
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'},
+        ),
+    ]

+ 2 - 2
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):

+ 2 - 2
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):
         """

+ 0 - 32
netbox/utilities/sql.py

@@ -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)