django-model-utils/tests
Skia 33e46ebd53 managers: avoid querying too much tables when not needed
When calling `select_related()` with an empty list of arguments [1], Django will
try to prefetch some data by doing some first level joints with the related
classes.
This can lead to obvious negative performance impact, but this also breaks some
workarounds for having inheritance for foreign keys [2], as those solutions rely
on lazy evaluation of the related object.

[1]: a4e6030904/django/db/models/query.py (L1051)
     Only passing an explicit `None` to `select_related` will disable the magic.
[2]: https://github.com/jazzband/django-model-utils/issues/11

As examples, here are the generated SQL requests in
InheritanceManagerRelatedTests.test_get_method_with_select_subclasses_check_for_useless_join:

  * without this fix, without adding `.select_related(None)`
```sql
SELECT
    "tests_inheritancemanagertestparent"."id",
    "tests_inheritancemanagertestparent"."non_related_field_using_descriptor",
    "tests_inheritancemanagertestparent"."related_id",
    "tests_inheritancemanagertestparent"."normal_field",
    "tests_inheritancemanagertestparent"."related_self_id",
    "tests_inheritancemanagertestchild4"."other_onetoone_id",
    "tests_inheritancemanagertestchild4"."parent_ptr_id", T3."id",
    T3."non_related_field_using_descriptor", T3."related_id", T3."normal_field",
    T3."related_self_id"
FROM
    "tests_inheritancemanagertestchild4"
INNER JOIN
    "tests_inheritancemanagertestparent" ON
    ("tests_inheritancemanagertestchild4"."parent_ptr_id" = "tests_inheritancemanagertestparent"."id")
INNER JOIN
    "tests_inheritancemanagertestparent" T3 ON
    ("tests_inheritancemanagertestchild4"."other_onetoone_id" = T3."id")
WHERE
    "tests_inheritancemanagertestchild4"."parent_ptr_id" = 191
```

  * with either the fix, or by adding `.select_related(None)` after `.select_subclasses()`
```sql
SELECT
    "tests_inheritancemanagertestparent"."id",
    "tests_inheritancemanagertestparent"."non_related_field_using_descriptor",
    "tests_inheritancemanagertestparent"."related_id",
    "tests_inheritancemanagertestparent"."normal_field",
    "tests_inheritancemanagertestparent"."related_self_id",
    "tests_inheritancemanagertestchild4"."other_onetoone_id",
    "tests_inheritancemanagertestchild4"."parent_ptr_id"
FROM
    "tests_inheritancemanagertestchild4"
INNER JOIN
    "tests_inheritancemanagertestparent" ON
    ("tests_inheritancemanagertestchild4"."parent_ptr_id" = "tests_inheritancemanagertestparent"."id")
WHERE
    "tests_inheritancemanagertestchild4"."parent_ptr_id" = 191

```
2020-07-08 16:59:05 +06:00
..
test_fields FieldTracker should patch save_base instead of save (#404) 2019-12-15 22:43:58 +06:00
test_managers managers: avoid querying too much tables when not needed 2020-07-08 16:59:05 +06:00
test_models Overriding TimeStampedModel's save() method (#412) 2020-01-23 13:19:06 +06:00
__init__.py Exclude tests from distribution 2017-02-15 23:00:10 +00:00
fields.py Add django 3.0 to the test matrix and drop six (#395) 2019-12-09 19:37:16 +06:00
managers.py Modernize Python syntax, add Python 3.8 (#398) 2019-11-14 22:50:04 +06:00
models.py FieldTracker should patch save_base instead of save (#404) 2019-12-15 22:43:58 +06:00
settings.py fix model.save patched in FieldTracker (#353) 2018-12-10 21:35:26 +06:00
signals.py Fixed spacing. 2019-08-19 22:38:05 +01:00
test_choices.py Modernize Python syntax, add Python 3.8 (#398) 2019-11-14 22:50:04 +06:00
test_inheritance_iterable.py Modernize Python syntax, add Python 3.8 (#398) 2019-11-14 22:50:04 +06:00
test_miscellaneous.py Modernize Python syntax, add Python 3.8 (#398) 2019-11-14 22:50:04 +06:00