Django model mixins and utilities.
Find a file
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
.github Pull request and issue template fixes #254 2017-01-17 22:34:04 +00:00
docs Document current supported versions 2020-06-11 21:24:11 +06:00
model_utils managers: avoid querying too much tables when not needed 2020-07-08 16:59:05 +06:00
tests managers: avoid querying too much tables when not needed 2020-07-08 16:59:05 +06:00
.coveragerc Add a JoinManager that helps with performance (#351) 2018-11-25 20:18:23 +06:00
.editorconfig Add EditorConfig file 2013-05-20 22:38:00 -07:00
.gitignore Improve Makefile to handle VENV creation if needed. 2019-08-20 12:09:51 +02:00
.hgignore Add runtests.sh script to run tox and generate HTML coverage summary. 2013-01-27 13:35:48 -08:00
.hgtags Added tag 1.1.0 for changeset 92792fb14a51 2012-04-13 17:33:51 -06:00
.travis.yml Modernize Python syntax, add Python 3.8 (#398) 2019-11-14 22:50:04 +06:00
AUTHORS.rst Prepare 4.0.0 release. 2019-12-11 15:47:18 +01:00
CHANGES.rst managers: avoid querying too much tables when not needed 2020-07-08 16:59:05 +06:00
CONTRIBUTING.rst Add jazzband badge/contributing guidelines. 2017-04-15 08:50:55 -07:00
LICENSE.txt Update year. 2019-01-10 15:38:56 -05:00
Makefile Improve Makefile to handle VENV creation if needed. 2019-08-20 12:09:51 +02:00
MANIFEST.in MANIFEST.in: Add docs and tests (#362) 2019-03-11 13:51:59 +06:00
README.rst Add django 3.0 to the test matrix and drop six (#395) 2019-12-09 19:37:16 +06:00
requirements-test.txt Modernize Python syntax, add Python 3.8 (#398) 2019-11-14 22:50:04 +06:00
requirements.txt Add requirement for running tests 2019-05-20 19:32:02 +02:00
setup.cfg switch to py.test + pytest-cov 2017-12-11 08:11:55 -06:00
setup.py Add django 3.0 to the test matrix and drop six (#395) 2019-12-09 19:37:16 +06:00
tox.ini Fix tox django factors 2020-04-28 21:36:51 +06:00
translations.py Remove unsupported django version hacks. 2019-08-21 11:02:23 +02:00

==================
django-model-utils
==================

.. image:: https://jazzband.co/static/img/badge.svg
   :target: https://jazzband.co/
   :alt: Jazzband
.. image:: https://travis-ci.org/jazzband/django-model-utils.svg?branch=master
   :target: https://travis-ci.org/jazzband/django-model-utils
.. image:: https://codecov.io/gh/jazzband/django-model-utils/branch/master/graph/badge.svg
  :target: https://codecov.io/gh/jazzband/django-model-utils
.. image:: https://img.shields.io/pypi/v/django-model-utils.svg
   :target: https://pypi.python.org/pypi/django-model-utils

Django model mixins and utilities.

``django-model-utils`` supports `Django`_ 2.1+ and 3.0+.

.. _Django: http://www.djangoproject.com/

This app is available on `PyPI`_.

.. _PyPI: https://pypi.python.org/pypi/django-model-utils/

Getting Help
============

Documentation for django-model-utils is available
https://django-model-utils.readthedocs.io/


Run tests
---------

.. code-block

    pip install -e .
    py.test

Contributing
============

Please file bugs and send pull requests to the `GitHub repository`_ and `issue
tracker`_. See `CONTRIBUTING.rst`_ for details.

.. _GitHub repository: https://github.com/jazzband/django-model-utils/
.. _issue tracker: https://github.com/jazzband/django-model-utils/issues
.. _CONTRIBUTING.rst: https://github.com/jazzband/django-model-utils/blob/master/CONTRIBUTING.rst