DjangoQL provides an advanced search language for Django admin, allowing users to construct complex queries using an intuitive syntax with autocomplete suggestions. Unfold automatically styles DjangoQL’s search interface to match your admin theme.
DjangoQL enables power users to perform complex searches without requiring custom filters, making data exploration faster and more flexible.
Once enabled, users can perform advanced searches:
# Find articles by specific authorauthor.username = "john"# Multiple conditionsstatus = "published" and published_date > "2024-01-01"# Complex queries(status = "draft" or status = "review") and author.username ~ "admin"# Null checkspublished_date is not null# Range queriesview_count >= 100 and view_count <= 1000
# Equalitystatus = "published"author.id = 123# Inequalitystatus != "draft"# Greater than / Less thanview_count > 100published_date < "2024-12-31"# Greater than or equal / Less than or equalrating >= 4.5price <= 99.99
# ANDstatus = "published" and view_count > 100# ORstatus = "draft" or status = "review"# NOTnot status = "archived"# Grouping with parentheses(status = "published" or status = "featured") and author.is_staff = True
# Specific datepublished_date = "2024-03-15"# Date rangepublished_date >= "2024-01-01" and published_date <= "2024-12-31"# This yearpublished_date >= "2024-01-01"
from djangoql.schema import DjangoQLSchemaclass ArticleQLSchema(DjangoQLSchema): include = (Article,) # Only allow searching Article model def get_fields(self, model): if model == Article: # Only allow searching these fields return ["title", "status", "published_date", "author"] return super().get_fields(model)@admin.register(Article)class ArticleAdmin(DjangoQLSearchMixin, ModelAdmin): djangoql_schema = ArticleQLSchema
@admin.register(Article)class ArticleAdmin(DjangoQLSearchMixin, ModelAdmin): # Regular search still works search_fields = ["title", "content"] # Users can toggle between regular and advanced search
class ArticleQLSchema(DjangoQLSchema): def get_field_suggestions(self, model, field_name): if model == Article and field_name == "author": # Return actual usernames from database from django.contrib.auth import get_user_model User = get_user_model() return list(User.objects.values_list("username", flat=True)[:10]) return super().get_field_suggestions(model, field_name)
Syntax highlighting: Query syntax with proper colors
Unfold removes the default DjangoQL documentation link for a cleaner interface. Users can still access query syntax through the autocomplete suggestions.
# Published articles with high engagementstatus = "published" and view_count > 1000 and published_date >= "2024-01-01"# Draft articles by specific authorsstatus = "draft" and (author.username = "john" or author.username = "jane")
User Management
Search users with specific attributes:
# Active staff membersis_active = True and is_staff = True# Users who haven't logged in recentlylast_login < "2024-01-01" or last_login is null# Users from specific domainemail ~$ "@company.com" and date_joined > "2023-01-01"
E-commerce Orders
Find orders matching criteria:
# Large pending ordersstatus = "pending" and total > 1000# Orders from VIP customerscustomer.tier = "vip" and created_date >= "2024-01-01"# Shipped orders with trackingstatus = "shipped" and tracking_number is not null
Data Analysis
Analyze records with complex filters:
# Products with low stock and high demandstock_quantity < 10 and sales_last_month > 50# Underperforming campaignscampaign.status = "active" and conversion_rate < 0.02