Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions modelcluster/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def get_clone(self, results = None):
new.tuple_fields = self.tuple_fields
new.iterable_class = self.iterable_class
return new

def resolve_q_object(self, q_object):
connector = q_object.connector
filters = []
Expand All @@ -405,14 +405,14 @@ def test_inner(obj):
else:
key_clauses, val = child
filters.append(_build_test_function_from_filter(self.model, key_clauses.split('__'), val))

return test(filters)

def _get_filters(self, *args, **kwargs):
# a list of test functions; objects must pass all tests to be included
# in the filtered list
filters = []

for q_object in args:
filters.append(self.resolve_q_object(q_object))

Expand Down Expand Up @@ -527,6 +527,12 @@ def distinct(self, *fields):
unique_results.append(result)
return self.get_clone(results=unique_results)

def none(self):
"""
Return an empty QuerySet.
"""
return self.get_clone(results=[])

# a standard QuerySet will store the results in _result_cache on running the query;
# this is effectively the same as self.results on a FakeQuerySet, and so we'll make
# _result_cache an alias of self.results for the benefit of Django internals that
Expand Down
29 changes: 29 additions & 0 deletions tests/tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,35 @@ def test_distinct_with_fields(self):
albums = [album.name for album in beatles.albums.order_by('sort_order').distinct('name')]
self.assertEqual(['Please Please Me', 'With The Beatles', 'Abbey Road'], albums)

def test_none(self):
beatles = Band(
name="The Beatles",
albums=[
Album(name="Please Please Me", sort_order=1),
Album(name="With The Beatles", sort_order=2),
Album(name="Abbey Road", sort_order=3),
],
)

# none() should return an empty queryset
self.assertEqual([], list(beatles.albums.none()))
self.assertEqual(0, beatles.albums.none().count())

def test_none_is_chainable(self):
beatles = Band(
name="The Beatles",
albums=[
Album(name="Please Please Me", sort_order=1),
Album(name="With The Beatles", sort_order=2),
],
)

# Chaining operations after none() should still return empty results
self.assertEqual(
[], list(beatles.albums.none().filter(name="Please Please Me"))
)
self.assertEqual([], list(beatles.albums.none().order_by("name")))

def test_parental_key_checks_clusterable_model(self):
from django.core import checks
from django.db import models
Expand Down
Loading