Closes #3094: Remove NullsFirstQuerySet

This commit is contained in:
Jeremy Stretch 2019-04-19 20:59:07 -04:00
parent 46b3512c45
commit e4c06700bb
4 changed files with 22 additions and 36 deletions

View File

@ -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'},
),
]

View File

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

View File

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

View File

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