Utilities¶
Utility functions and helpers.
Query Parameters¶
- django_api_orm.utils.build_query_params(**filters)[source]¶
Build query parameters from filter kwargs.
Example Usage¶
from django_api_orm.utils import build_query_params
# Build query parameters
params = build_query_params(
active=True,
role="admin",
limit=10,
offset=0
)
# {"active": "true", "role": "admin", "limit": "10", "offset": "0"}
# None values are excluded
params = build_query_params(
active=True,
role=None,
limit=10
)
# {"active": "true", "limit": "10"}
Dictionary Operations¶
- django_api_orm.utils.merge_dicts(*dicts)[source]¶
Merge multiple dictionaries, with later dicts taking precedence.
Example Usage¶
from django_api_orm.utils import merge_dicts
# Merge multiple dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict3 = {"c": 5, "d": 6}
merged = merge_dicts(dict1, dict2, dict3)
# {"a": 1, "b": 3, "c": 5, "d": 6}
# Later values override earlier ones
merged = merge_dicts({"a": 1}, {"a": 2}, {"a": 3})
# {"a": 3}
List Operations¶
- django_api_orm.utils.chunk_list(items, chunk_size)[source]¶
Split a list into chunks of specified size.
Example Usage¶
from django_api_orm.utils import chunk_list
# Chunk a list into smaller lists
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunks = chunk_list(items, chunk_size=3)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
# Process in batches
for batch in chunk_list(user_ids, chunk_size=100):
# Process batch of up to 100 user IDs
pass
See Also¶
Source code in
src/django_api_orm/utils.py