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

Usage

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())

Django admin integration

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

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.1.1.tar.gz (8.5 kB view hashes)

Uploaded Source

Supported by

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