# dj-raw-sql
[![Dependencies](https://img.shields.io/librariesio/github/axemanofic/dj-raw-sql)](https://pypi.org/project/dj-raw-sql/)
[![Version](https://img.shields.io/pypi/v/dj-raw-sql?color=green)](https://pypi.org/project/dj-raw-sql/)
[![Downloads](https://pepy.tech/badge/dj-raw-sql/month)](https://pepy.tech/project/dj-raw-sql)
[![Downloads](https://pepy.tech/badge/dj-raw-sql/week)](https://pepy.tech/project/dj-raw-sql)
dj-raw-sql is just a wrapper over the [standard Django query](https://docs.djangoproject.com/en/4.1/topics/db/sql/#executing-custom-sql-directly)
This demo shows how to get the record(s) from the database
Example:
``` py title="queries.py" linenums="1"
def get_music_by_id(id: int):
return "SELECT * FROM dj_app_music WHERE id = %s", (id,)
```
``` py title="models.py" linenums="1"
from django.db import models
# Our demo model
class Music(models.Model):
name = models.CharField(max_length=150)
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
is_delete = models.BooleanField(default=False)
```
``` py title="views.py" linenums="1"
from django.http import JsonResponse
from django.views import View
from my_app.queries import get_music_by_id
from dj_raw_sql import QueryExecutor
class MyView(View):
def get(self, request, *args, **kwargs):
music: tuple[tuple] = QueryExecutor.fetchone(get_music_by_id, id=1)
return JsonResponse({"name": music[0][1]})
```
## Benchmarks
**Q**: How were performance tests conducted?
**A**: tests/test_collection/ performance tests are located here. A dataset of 5000 elements was generated and loaded into the database. Then the query "SELECT * FROM dj_app_music LIMIT %s" was called, where the value of LIMIT changed from 10 to 5000 in each test.
---
Test results
| Number of items | fetchall | to_ordereddict=True |
|-----------------|:-------------:|:-------------------:|
| 10 | 0.00006 | 0.00011 |
| 100 | 0.00017 | 0.00025 |
| 1000 | 0.00138 | 0.00207 |
| 5000 | 0.00658 | 0.01052 |
## Improve project
If you want to improve the project then create "Issues" . If you want to help with writing tests or typing, create a "pull request".
Raw data
{
"_id": null,
"home_page": "https://github.com/axemanofic/dj-raw-sql",
"name": "dj-raw-sql",
"maintainer": "Roman",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "axeman.ofic@gmail.com",
"keywords": "python,django,sql,database,query,web",
"author": "Roman Sotnikov",
"author_email": "axeman.ofic@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b9/12/0d05a0d79369eb36d7a19b7827dab8d91194748d4cb6674f1c63c50bd3ec/dj_raw_sql-3.0.0.tar.gz",
"platform": null,
"description": "# dj-raw-sql\n\n[![Dependencies](https://img.shields.io/librariesio/github/axemanofic/dj-raw-sql)](https://pypi.org/project/dj-raw-sql/)\n[![Version](https://img.shields.io/pypi/v/dj-raw-sql?color=green)](https://pypi.org/project/dj-raw-sql/)\n[![Downloads](https://pepy.tech/badge/dj-raw-sql/month)](https://pepy.tech/project/dj-raw-sql)\n[![Downloads](https://pepy.tech/badge/dj-raw-sql/week)](https://pepy.tech/project/dj-raw-sql)\n\ndj-raw-sql is just a wrapper over the [standard Django query](https://docs.djangoproject.com/en/4.1/topics/db/sql/#executing-custom-sql-directly)\n\nThis demo shows how to get the record(s) from the database\n\nExample:\n\n``` py title=\"queries.py\" linenums=\"1\"\ndef get_music_by_id(id: int):\n return \"SELECT * FROM dj_app_music WHERE id = %s\", (id,)\n```\n\n``` py title=\"models.py\" linenums=\"1\"\nfrom django.db import models\n\n# Our demo model\nclass Music(models.Model):\n name = models.CharField(max_length=150)\n create_at = models.DateTimeField(auto_now_add=True)\n update_at = models.DateTimeField(auto_now=True)\n is_delete = models.BooleanField(default=False)\n```\n\n``` py title=\"views.py\" linenums=\"1\"\nfrom django.http import JsonResponse\nfrom django.views import View\n\nfrom my_app.queries import get_music_by_id\n\nfrom dj_raw_sql import QueryExecutor\n\n\nclass MyView(View):\n def get(self, request, *args, **kwargs):\n music: tuple[tuple] = QueryExecutor.fetchone(get_music_by_id, id=1)\n return JsonResponse({\"name\": music[0][1]})\n```\n\n## Benchmarks\n\n**Q**: How were performance tests conducted?\n\n**A**: tests/test_collection/ performance tests are located here. A dataset of 5000 elements was generated and loaded into the database. Then the query \"SELECT * FROM dj_app_music LIMIT %s\" was called, where the value of LIMIT changed from 10 to 5000 in each test.\n\n---\nTest results\n\n| Number of items | fetchall | to_ordereddict=True |\n|-----------------|:-------------:|:-------------------:|\n| 10 | 0.00006 | 0.00011 |\n| 100 | 0.00017 | 0.00025 |\n| 1000 | 0.00138 | 0.00207 |\n| 5000 | 0.00658 | 0.01052 |\n\n## Improve project\n\nIf you want to improve the project then create \"Issues\" . If you want to help with writing tests or typing, create a \"pull request\".\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "This is a Django wrapper to make it easier to write raw SQL queries.",
"version": "3.0.0",
"project_urls": {
"Documentation": "https://axemanofic.github.io/dj-raw-sql/",
"Homepage": "https://github.com/axemanofic/dj-raw-sql",
"Repository": "https://github.com/axemanofic/dj-raw-sql"
},
"split_keywords": [
"python",
"django",
"sql",
"database",
"query",
"web"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2dd527a3c8025d15adfce72904c7c6e47876497aff43d2a9c096399ab1184839",
"md5": "7f976215d7c500e24db52b2743848451",
"sha256": "59c06b5b63a790a190dd6e126212255b37bb3f12aa0ba6b594c0b838a1b19c8e"
},
"downloads": -1,
"filename": "dj_raw_sql-3.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7f976215d7c500e24db52b2743848451",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 4959,
"upload_time": "2023-12-08T19:31:15",
"upload_time_iso_8601": "2023-12-08T19:31:15.758441Z",
"url": "https://files.pythonhosted.org/packages/2d/d5/27a3c8025d15adfce72904c7c6e47876497aff43d2a9c096399ab1184839/dj_raw_sql-3.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b9120d05a0d79369eb36d7a19b7827dab8d91194748d4cb6674f1c63c50bd3ec",
"md5": "24a120a5fba4c4f9e89ac11b9d35e470",
"sha256": "3746d746223202bd9b8848b0d1f6ebb278643ba85440032fc3d9533ab2ae57ad"
},
"downloads": -1,
"filename": "dj_raw_sql-3.0.0.tar.gz",
"has_sig": false,
"md5_digest": "24a120a5fba4c4f9e89ac11b9d35e470",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 4754,
"upload_time": "2023-12-08T19:31:17",
"upload_time_iso_8601": "2023-12-08T19:31:17.496054Z",
"url": "https://files.pythonhosted.org/packages/b9/12/0d05a0d79369eb36d7a19b7827dab8d91194748d4cb6674f1c63c50bd3ec/dj_raw_sql-3.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-08 19:31:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "axemanofic",
"github_project": "dj-raw-sql",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "dj-raw-sql"
}