No project description provided
Project description
django-query-tools
Tools for building queries in Django.
Setup
$ pip install django-query-tools
To use the server
module, Django must also be installed:
$ pip install django
Usage
# script.py
from django_query_tools.client import F
from django_query_tools.server import make_atoms, make_query
from django.db.models import Q
# --- CLIENT ---
# Define x and y fields
x = F(x="hello")
print("F object for x:", x)
print("Data:", x.query)
y = F(y="goodbye")
print("F object for y:", y)
print("Data:", y.query)
# Form a query from x and y
query = x & ~y
print("Query F object:", query)
print("Client sends data:", query.query)
# --- SERVER ---
# Server receives query
data = query.query
print("Server receives data:", data)
# Modifies data in-place, creating QueryAtom keyvalue pairs
# Returns these QueryAtom objects for validation
atoms = make_atoms(data)
print("QueryAtom structure:", data)
print("QueryAtom objects:", atoms)
# Builds Django Q object from the QueryAtom objects
formed = make_query(data)
expected = Q(x="hello") & ~Q(y="goodbye")
print("Formed Q object:", formed)
print("Expected Q object:", expected)
print("These objects represent the same query:", formed == expected)
$ python script.py
F object for x: <django_query_tools.client.F object at 0x1012f1910>
Data: {'x': 'hello'}
F object for y: <django_query_tools.client.F object at 0x1012f1990>
Data: {'y': 'goodbye'}
Query F object: <django_query_tools.client.F object at 0x1012f3510>
Client sends data: {'&': [{'x': 'hello'}, {'~': {'y': 'goodbye'}}]}
Server receives data: {'&': [{'x': 'hello'}, {'~': {'y': 'goodbye'}}]}
QueryAtom structure: {'&': [{'x': <django_query_tools.server.QueryAtom object at 0x1012f1a10>}, {'~': {'y': <django_query_tools.server.QueryAtom object at 0x1023b1890>}}]}
QueryAtom objects: [<django_query_tools.server.QueryAtom object at 0x1012f1a10>, <django_query_tools.server.QueryAtom object at 0x1023b1890>]
Formed Q object: (AND: ('x', 'hello'), (NOT (AND: ('y', 'goodbye'))))
Expected Q object: (AND: ('x', 'hello'), (NOT (AND: ('y', 'goodbye'))))
These objects represent the same query: True
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
Built Distribution
File details
Details for the file django_query_tools-0.3.3.tar.gz
.
File metadata
- Download URL: django_query_tools-0.3.3.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54f3019320cf6f61284318103cd356b444df7e33eb567d55e59103c3516e5361 |
|
MD5 | 7ac5e943fab82c7aa53f9cf4122728d4 |
|
BLAKE2b-256 | 33fe568613afa53ea76eb8fede4bab89783870df1112eb724b2da16bf797849a |
File details
Details for the file django_query_tools-0.3.3-py3-none-any.whl
.
File metadata
- Download URL: django_query_tools-0.3.3-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9da1995c5d75c7895e58ce3cf7aabe6249fa4dd22cfa6d096f0c70c62c274570 |
|
MD5 | 3c60aab9cfc7b6ac01c47d351733f0b4 |
|
BLAKE2b-256 | 10d50f55603d7e9baa9d5cfe4912d2959bc74399a03be81d92e30eddb03cc465 |