Skip to main content

DjangoQL: query mini-language that translates to Django ORM

Project description

https://travis-ci.org/ivelum/djangoql.svg?branch=master

Query mini-language that translates into Django ORM. Supports logical operators, parenthesis, table joins, works with any Django models. Tested vs. Python 2.7, 3.5 and 3.6, Django 1.8, 1.9, 1.10.

Installation

$ pip install djangoql

Add 'djangoql' to INSTALLED_APPS in your settings.py:

INSTALLED_APPS = [
    ...
    'djangoql',
    ...
]

Add it to your Django admin

Add DjangoQLSearchMixin to your model admin, and it will replace standard Django search functionality with DjangoQL search. Example:

from django.contrib import admin

from djangoql.admin import DjangoQLSearchMixin

from .models import Book


@admin.register(Book)
class BookAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
    pass

Can I use it outside of Django admin?

Sure. You can add DjangoQL search functionality to any Django model using DjangoQLQuerySet:

from django.db import models

from djangoql.queryset import DjangoQLQuerySet


class Book(models.Model):
    name = models.CharField(max_length=255)
    author = models.ForeignKey('auth.User')

    objects = DjangoQLQuerySet.as_manager()

With the example above you can perform search like this:

qs = Book.objects.djangoql(
    'name ~ "war" and author.last_name = "Tolstoy"'
)

It returns a normal queryset, so you can extend it and reuse if necessary. The following code works fine:

print(qs.count())

Alternatively you can add DjangoQL search to any existing queryset, even if it’s not an instance of DjangoQLQuerySet:

from django.contrib.auth.models import User

from djangoql.queryset import apply_search

qs = User.objects.all()
qs = apply_search(qs, 'groups = None')
print(qs.exists())

Language reference

DjangoQL looks close to Python syntax, however there’re some minor differences. Basically you just reference model fields like you do it in Python code, apply comparison and logical operators and parenthesis. DjangoQL is case-sensitive.

  • model fields: exactly as they are defined in Python code. Access nested properties via ., for example author.last_name;

  • strings must be double-quoted. Single quotes are not supported. To escape a double quote use \";

  • boolean and null values: True, False, None. Please note that they can be combined with equality operators only, so you can write published = False or date_published = None, but published > False will cause an error;

  • logical operators: and, or;

  • comparison operators: =, !=, <, <=, >, >= - work as you expect. ~ and !~ - test that a string contains or not contains a substring (translated into __icontains);

  • test a value vs. list: in, not in. Example: pk in (2, 3).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangoql-0.3.2.tar.gz (17.4 kB view details)

Uploaded Source

File details

Details for the file djangoql-0.3.2.tar.gz.

File metadata

  • Download URL: djangoql-0.3.2.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for djangoql-0.3.2.tar.gz
Algorithm Hash digest
SHA256 4377dcc4e0b3b7096ae98eb5f3bf8b18a3021721f533347ce0aa1a4696ea4438
MD5 aa9493d72702a99682bc35d0e28a2042
BLAKE2b-256 b4dc6b7ef25e363033414ffcb771ee01450a8aa58c686f0da89468b58043c4c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page