django-query-limiter


Namedjango-query-limiter JSON
Version 0.2.0 PyPI version JSON
download
home_page
SummaryLimit django query amounts in a certain scope
upload_time2024-02-18 19:37:14
maintainer
docs_urlNone
authorRaven De Vooght
requires_python
licenseBSD 3-Clause License Copyright (c) 2024, TWRaven Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
keywords django query limit performance
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Ever wanted to limit how many db queries a certain function or view can make? 
You're in luck!

Get it from [PyPi](https://pypi.org/project/django-query-limiter/):
```shell
pip install django-query-limiter[formatting]
```

## Basic usage

```python
from your_django_models import Model
from query_limiter import limit_queries

with limit_queries(1):
    print(Model.objects.first()) # Prints your model's first object

with limit_queries(1):
    print(Model.objects.first()) # Prints your model's first object
    print(Model.objects.first()) # Raises QueryLimitError
```

It can also be used as a function decorator, and can limit queries on a per-database level:
```python
from your_django_models import Model
from query_limiter import limit_queries

@limit_queries(10, db_connections=['default'])
def my_view(request):
    return Model.objects.first()
```

Multiple limits can be specified, and the first one to be reached will raise a `QueryLimitExceeded` exception:
```python
from your_django_models import Model
from query_limiter import limit_queries
from datetime import timedelta

@limit_queries(
    amount=2, 
    individual_max_time=timedelta(milliseconds=100), 
    total_max_time=timedelta(seconds=1),
)
def my_view(request):
    Model.objects.first() # if this query takes more than 100ms -> QueryLimitExceeded
    Model.objects.first() # if the total time of all queries takes more than 1s -> QueryLimitExceeded
    Model.objects.first() # if this query is reached -> QueryLimitExceeded
```

You can globally disable the query limiter:
```python
from your_django_models import Model
from query_limiter import limit_queries, disable_query_limiter

if settings.ENVIROMENT == 'production':
    disable_query_limiter()

with limit_queries(1):
    print(Model.objects.first())  # Prints your model's first object
    print(Model.objects.first())  # Prints your model's first object
```

The default behaviour is to limit the connections to all databases. This can be globally overwriten like so:
```python
from query_limiter import set_default_databases

set_default_databases(['your_database_name', 'another_database_name'])
```
Unless the db connections are specified in the `limit_queries` decorator, these databases will be used.



### Formatting

The `formatting` extra:
 - humanizes all timedelta outputs
 - uses `sqlparse` to format the queries to be more human readable

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-query-limiter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "django,query,limit,performance",
    "author": "Raven De Vooght",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/a4/17/86e48cc0efde924a9777dc05abf68543e8c9283eaaab4dc065784f00fc2d/django-query-limiter-0.2.0.tar.gz",
    "platform": null,
    "description": "Ever wanted to limit how many db queries a certain function or view can make? \nYou're in luck!\n\nGet it from [PyPi](https://pypi.org/project/django-query-limiter/):\n```shell\npip install django-query-limiter[formatting]\n```\n\n## Basic usage\n\n```python\nfrom your_django_models import Model\nfrom query_limiter import limit_queries\n\nwith limit_queries(1):\n    print(Model.objects.first()) # Prints your model's first object\n\nwith limit_queries(1):\n    print(Model.objects.first()) # Prints your model's first object\n    print(Model.objects.first()) # Raises QueryLimitError\n```\n\nIt can also be used as a function decorator, and can limit queries on a per-database level:\n```python\nfrom your_django_models import Model\nfrom query_limiter import limit_queries\n\n@limit_queries(10, db_connections=['default'])\ndef my_view(request):\n    return Model.objects.first()\n```\n\nMultiple limits can be specified, and the first one to be reached will raise a `QueryLimitExceeded` exception:\n```python\nfrom your_django_models import Model\nfrom query_limiter import limit_queries\nfrom datetime import timedelta\n\n@limit_queries(\n    amount=2, \n    individual_max_time=timedelta(milliseconds=100), \n    total_max_time=timedelta(seconds=1),\n)\ndef my_view(request):\n    Model.objects.first() # if this query takes more than 100ms -> QueryLimitExceeded\n    Model.objects.first() # if the total time of all queries takes more than 1s -> QueryLimitExceeded\n    Model.objects.first() # if this query is reached -> QueryLimitExceeded\n```\n\nYou can globally disable the query limiter:\n```python\nfrom your_django_models import Model\nfrom query_limiter import limit_queries, disable_query_limiter\n\nif settings.ENVIROMENT == 'production':\n    disable_query_limiter()\n\nwith limit_queries(1):\n    print(Model.objects.first())  # Prints your model's first object\n    print(Model.objects.first())  # Prints your model's first object\n```\n\nThe default behaviour is to limit the connections to all databases. This can be globally overwriten like so:\n```python\nfrom query_limiter import set_default_databases\n\nset_default_databases(['your_database_name', 'another_database_name'])\n```\nUnless the db connections are specified in the `limit_queries` decorator, these databases will be used.\n\n\n\n### Formatting\n\nThe `formatting` extra:\n - humanizes all timedelta outputs\n - uses `sqlparse` to format the queries to be more human readable\n",
    "bugtrack_url": null,
    "license": "BSD 3-Clause License  Copyright (c) 2024, TWRaven  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ",
    "summary": "Limit django query amounts in a certain scope",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/TWRaven/django-query-limiter"
    },
    "split_keywords": [
        "django",
        "query",
        "limit",
        "performance"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eaa70847aa649593e173b450c750cd779a36d83f30f2c14b7f8fb72c4dc060a3",
                "md5": "5c3a8eee4bbc4f3624aae81b73b34e1a",
                "sha256": "966953759ee95080cdb787937d51da1e14e9082338e0e45871262a3d2fa56211"
            },
            "downloads": -1,
            "filename": "django_query_limiter-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5c3a8eee4bbc4f3624aae81b73b34e1a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6598,
            "upload_time": "2024-02-18T19:37:12",
            "upload_time_iso_8601": "2024-02-18T19:37:12.733952Z",
            "url": "https://files.pythonhosted.org/packages/ea/a7/0847aa649593e173b450c750cd779a36d83f30f2c14b7f8fb72c4dc060a3/django_query_limiter-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a41786e48cc0efde924a9777dc05abf68543e8c9283eaaab4dc065784f00fc2d",
                "md5": "483a2d85f60a90311f2d27029bb1a34b",
                "sha256": "a072558c69bff7fdf920655d5ad6fe36475c611fb6e03a34ce2f80051622a848"
            },
            "downloads": -1,
            "filename": "django-query-limiter-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "483a2d85f60a90311f2d27029bb1a34b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4814,
            "upload_time": "2024-02-18T19:37:14",
            "upload_time_iso_8601": "2024-02-18T19:37:14.201786Z",
            "url": "https://files.pythonhosted.org/packages/a4/17/86e48cc0efde924a9777dc05abf68543e8c9283eaaab4dc065784f00fc2d/django-query-limiter-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-18 19:37:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TWRaven",
    "github_project": "django-query-limiter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "django-query-limiter"
}
        
Elapsed time: 0.22531s