Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/paul-wolf/djaq
Django queries
https://github.com/paul-wolf/djaq
django-application django-models django-orm djaq-query query-language
Last synced: 28 days ago
JSON representation
Django queries
- Host: GitHub
- URL: https://github.com/paul-wolf/djaq
- Owner: paul-wolf
- License: mit
- Created: 2018-11-16T08:22:42.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-05-03T10:40:41.000Z (over 1 year ago)
- Last Synced: 2024-09-28T16:23:43.490Z (about 1 month ago)
- Topics: django-application, django-models, django-orm, djaq-query, query-language
- Language: Python
- Homepage:
- Size: 996 KB
- Stars: 75
- Watchers: 9
- Forks: 19
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
- -awesome-django - djaq - An instant remote API to Django models with a powerful query language. (Third-Party Packages / APIs)
- awesome-django - djaq - An instant remote API to Django models with a powerful query language. (Third-Party Packages / APIs)
README
Djaq
====|Python tests| |RTD build| |Python versions| |PyPi version|
.. |Python tests| image:: https://github.com/paul-wolf/djaq/actions/workflows/run_unit_tests.yml/badge.svg
:target: https://github.com/paul-wolf/djaq/actions/workflows/run_unit_tests.yml
:alt: Unit test status
.. |RTD build| image:: https://readthedocs.org/projects/djaq/badge/?version=latest
:target: https://djaq.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status.. |Python versions| image:: https://img.shields.io/pypi/pyversions/djaq?color=brightgreen
:alt: PyPI - Python Version.. |PyPi version| image:: https://badge.fury.io/py/Djaq.svg
:target: https://badge.fury.io/py/Djaq
:alt: PyPi VersionDjaq - pronounced “Jack” - is an alternative to the Django QuerySet API.
What sets it apart:
* No need to import models
* Clearer, more natural query syntax
* More powerful expressions
* More consistent query syntax without resorting to idiosyncratic methods like
``F()`` expressions, ``annotate()``, ``aggregate()``
* Column expressions are entirely evaluated in the database
* Extensible: you can write your own functions
* Pandas: Easily turn a query into Pandas DataframeThere is also a JSON representation of queries, so you can send queries from a
client. It's an instant API to your data. No need to write backend classes and
serializers.Djaq queries are strings. A query string for our example dataset might
look like this:.. code:: shell
DQ("Book", "name as title, publisher.name as publisher").go()
This retrieves a list of book titles with book publisher. But you can
formulate far more sophisticated queries; see below. You can send Djaq
queries from any language, Java, Javascript, golang, etc. to a Django
application and get results as JSON. In contrast to REST frameworks,
like TastyPie or Django Rest Framework (DRF), you have natural access to
the Django ORM from the client.Djaq sits on top of the Django ORM. It can happily be used alongside
QuerySets.- `Documentation `__
- `Installation `__
- `Settings `__
- `Query
Usage `__
- `Sample
Project `__Here's an example comparison between Djaq and Django QuerySets that gets every
publisher and counts the books for each that are above and below a rating
threshold... code:: python
DQ("Book", """publisher.name,
sumif(rating < 3, 1, 0) as below_3,
sumif(rating >= 3, 1, 0) as above_3
""")compared to QuerySet:
.. code:: python
from django.db.models import Count, Q
above_3 = Count('book', filter=Q(book__rating__gt=3))
below_3 = Count('book', filter=Q(book__rating__lte=3))
Publisher.objects.annotate(below_3=below_3).annotate(above_3=above_3)Get average, maximum, minimum price of books:
.. code:: python
DQ("Book", "avg(price), max(price), min(price)")
compared to QuerySet:
.. code:: python
Book.objects.aggregate(Avg('price'), Max('price'), Min('price'))
Get the difference from the average off the maximum price for each publisher:
.. code:: python
DQ("Book", "publisher.name, max(price) - avg(price) as price_diff")
compared to QuerySet:
.. code:: python
from django.db.models import Avg, Max
Book.objects.values("publisher__name") \
.annotate(price_diff=Max('price') - Avg('price'))