Skip to content

Commit

Permalink
relax OrderedModelManager check to a Warning if it returns correct Or…
Browse files Browse the repository at this point in the history
…deredModelQuerySet
  • Loading branch information
shuckc committed Mar 17, 2023
1 parent 743cb6d commit 7b9c89f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Change log
Unreleased
----------

3.7.4 - 2023-03-17
----------
- Relax Check for `OrderedModelManager` to a `Warning`, if the manager returns the correct queryset (#290)


3.7.3 - 2023-03-15
----------
- Restrict signal handler 'senders' to subclasses of `OrderedModelBase` to avoid query count regression due to `Collector.can_fast_delete` logic in `models/deletion.py` (#288)
Expand Down
25 changes: 18 additions & 7 deletions ordered_model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,25 @@ def check(cls, **kwargs):
)
)
if not issubclass(cls.objects.__class__, OrderedModelManager):
errors.append(
checks.Error(
"OrderedModelBase subclass has a ModelManager that does not inherit from OrderedModelManager.",
obj=str(cls.__qualname__),
id="ordered_model.E003",
# Not using our Manager. This is an Error if the queryset is also wrong, or
# a Warning if our own QuerySet is returned.
if issubclass(cls.objects.none().__class__, OrderedModelQuerySet):
errors.append(
checks.Warning(
"OrderedModelBase subclass has a ModelManager that does not inherit from OrderedModelManager. This is not ideal but will work.",
obj=str(cls.__qualname__),
id="ordered_model.W003",
)
)
)
if not issubclass(cls.objects.none().__class__, OrderedModelQuerySet):
else:
errors.append(
checks.Error(
"OrderedModelBase subclass has a ModelManager that does not inherit from OrderedModelManager.",
obj=str(cls.__qualname__),
id="ordered_model.E003",
)
)
elif not issubclass(cls.objects.none().__class__, OrderedModelQuerySet):
errors.append(
checks.Error(
"OrderedModelBase subclass ModelManager did not return a QuerySet inheriting from OrderedModelQuerySet.",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
name="django-ordered-model",
long_description=long_description,
long_description_content_type="text/markdown",
version="3.7.3",
version="3.7.4",
description="Allows Django models to be ordered and provides a simple admin interface for reordering them.",
author="Ben Firshman",
author_email="[email protected]",
Expand Down
17 changes: 16 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ class TestModel(OrderedModel):
)

def test_bad_manager(self):
class BadModelManager(models.Manager.from_queryset(OrderedModelQuerySet)):
class BadModelManager(models.Manager.from_queryset(models.QuerySet)):
pass

class TestModel(OrderedModel):
Expand All @@ -1393,6 +1393,21 @@ class TestModel(OrderedModel):
],
)

def test_builtin_manager_to_queryset(self):
class TestModel(OrderedModel):
objects = OrderedModelQuerySet.as_manager()

self.assertEqual(
checks.run_checks(app_configs=self.apps.get_app_configs()),
[
checks.Warning(
msg="OrderedModelBase subclass has a ModelManager that does not inherit from OrderedModelManager. This is not ideal but will work.",
obj="ChecksTest.test_builtin_manager_to_queryset.<locals>.TestModel",
id="ordered_model.W003",
)
],
)

def test_bad_queryset(self):
# I've swapped the inheritance order here so that the models.QuerySet is returned
class BadQSModelManager(
Expand Down

0 comments on commit 7b9c89f

Please sign in to comment.