{ "info": { "author": "Miroslav Shubernetskiy", "author_email": "UNKNOWN", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4" ], "description": "===============\nDjango SubQuery\n===============\n\n.. image:: https://badge.fury.io/py/django-sub-query.png\n :target: http://badge.fury.io/py/django-sub-query\n\n.. image:: https://travis-ci.org/miki725/django-sub-query.png?branch=master\n :target: https://travis-ci.org/miki725/django-sub-query\n\nDjango app which uses SQL sub-queries to solve some ORM limitations\n\n* Free software: MIT license\n* GitHub: https://github.com/miki725/django-sub-query\n\nWhy?\n----\n\nDjango ORM is pretty awesome however it has some limitations.\nOne of such limitations is the ability to use sub-queries.\nActually thats not 100% true since Django ORM can use nested queries\nhowever those subqueries are for either aggregates or are manually\nprovided by the user via ``QuerySet.extra()``. Such types of subqueries\nhowever sometimes are not good enough. One such scenario is when\nyou need to sort by one column however and at the same time you need to use\ndistinct on a different column. The usual solution is to use subquery\nwhere inner query will use distinct and outer query will do the sorting::\n\n SELECT *\n FROM (\n SELECT DISTINCT ON (\"table\".\"foo\") \n FROM \"table\"\n ) \"table\"\n ORDER BY \"table\".\"bar\" ASC;\n\nThe above however is not supported by vanilla Django ORM.\nYou can of course use raw SQL queries however that is not desired since\nthen you loose all of the ORM power (e.g. pagination, etc).\n\nOne hack to still use ORM and yet use subquery is use Django ORM\nto construct inner query and then manually add outer query::\n\n query = Model.objects.filter(...).distinct(...).query\n sql, params = query.sql_with_params()\n Model.objects.raw(\n 'SELECT * FROM ({}) \"table\" ORDER BY \"table\".\"foo\"'.format(sql),\n params\n )\n\nThis approach however is also not desired since due to:\n\n* not able to use ``select_related()`` when related table\n has similar column name(s)\n* still difficult to do pagination\n* more logic required to be able to do ``.count()``\n\nSolution\n--------\n\nAs you saw above, currently it is difficult to use sub-queries in Django\nhowever conceptually the solution is pretty simply since we should be able\nto simply wrap SQL generated by Django ORM within an outer query.\nThis is exactly what this library does. It hooks up directly into Django's\n``as_sql()`` ORM method which is responsible for generating SQL and adds\nouter query when necessary conditions are met.\n\nInstalling\n----------\n\nYou can install ``django-sub-query`` using pip::\n\n $ pip install django-sub-query\n\nUsing\n-----\n\nSince this library changes a few bits in inner-workings in Django ORM,\nthere is no simple way to use this library other then completely change\ndatabase engine::\n\n DATABASES = {\n 'default': {\n 'ENGINE': 'sub_query.db.backends.postgis',\n ...\n },\n }\n\nCurrently only ``postgis`` backend is supported however please feel free to\nopen an issue to add support for other backends (or contribute implementation!).\n\nOnce the database engine is changed, then you have to make sure the\nqueryset you are using is a subclass of ``SubQueryGeoQuerySet``::\n\n class ExampleModel(models.Model):\n ...\n objects = SubQueryGeoQuerySet.as_manager()\n\nTesting\n-------\n\nTo run the tests you need to install testing requirements first::\n\n $ make install\n\nThen to run tests, you can use ``manage.py`` or simply use Makefile command::\n\n $ python manage.py test\n # or\n $ make test\n\n\n\n\nHistory\n-------\n\n0.1.0 (2015-01-25)\n~~~~~~~~~~~~~~~~~~~~~\n\n* First release on PyPI.\n\n\nCredits\n-------\n\nDevelopment Lead\n~~~~~~~~~~~~~~~~\n\n* Miroslav Shubernetskiy - https://github.com/miki725\n\nContributors\n~~~~~~~~~~~~\n\nNone yet. Why not be the first?\n\n\nLicense\n-------\n\nThe MIT License (MIT)\n\nCopyright (c) 2015, Miroslav Shubernetskiy\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/miki725/django-sub-query", "keywords": "django", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "django-sub-query", "package_url": "https://pypi.org/project/django-sub-query/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-sub-query/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/miki725/django-sub-query" }, "release_url": "https://pypi.org/project/django-sub-query/0.1/", "requires_dist": null, "requires_python": null, "summary": "Django app which uses SQL sub-queries to solve some ORM limitations", "version": "0.1" }, "last_serial": 1396157, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "3e71b565a88e9922cf799dcefc150802", "sha256": "1fcc95fe2fd761045c490c9b364e4a26727ba6cee59e64b2454c13e957d5777f" }, "downloads": -1, "filename": "django-sub-query-0.1.tar.gz", "has_sig": false, "md5_digest": "3e71b565a88e9922cf799dcefc150802", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16960, "upload_time": "2015-01-25T23:22:20", "url": "https://files.pythonhosted.org/packages/12/55/3eb7e4d6c5be42c6467961931ca995aa45045de7fad36ac7ff4414a0525f/django-sub-query-0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "3e71b565a88e9922cf799dcefc150802", "sha256": "1fcc95fe2fd761045c490c9b364e4a26727ba6cee59e64b2454c13e957d5777f" }, "downloads": -1, "filename": "django-sub-query-0.1.tar.gz", "has_sig": false, "md5_digest": "3e71b565a88e9922cf799dcefc150802", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16960, "upload_time": "2015-01-25T23:22:20", "url": "https://files.pythonhosted.org/packages/12/55/3eb7e4d6c5be42c6467961931ca995aa45045de7fad36ac7ff4414a0525f/django-sub-query-0.1.tar.gz" } ] }