Skip to main content

A Django admin widget for building complex queries using jQuery QueryBuilder

Project description

Django Query Builder Widget

A Django admin widget that integrates jQuery QueryBuilder for building complex database queries through an intuitive visual interface.

Preview

image

Features

  • Visual query building interface in Django admin
  • Automatic field detection from Django models
  • Support for all major Django field types
  • Customizable operators per field type
  • Support for custom fields and operators
  • JSON-based query storage
  • Works seamlessly with Django admin

Installation

Install via pip:

pip install django-query-builder-widget

Quick Start

1. Add to your Django project

Add the app to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ...
    'django_query_builder_widget',
    # ...
]

2. Collect static files

python manage.py collectstatic

3. Use in your admin

from django.contrib import admin
from django_query_builder_widget import QueryBuilderWidget
from .models import YourModel

class YourModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.JSONField: {'widget': QueryBuilderWidget(model=YourModel)},
    }

admin.site.register(YourModel, YourModelAdmin)

Usage Examples

Basic Usage with Model

from django_query_builder_widget import QueryBuilderWidget

# Auto-generate filters from model fields
widget = QueryBuilderWidget(model=MyModel)

Specify Specific Fields

# Only include specific fields
widget = QueryBuilderWidget(
    model=MyModel,
    fields=['name', 'email', 'age', 'created_at']
)

Custom Field Configuration

widget = QueryBuilderWidget(
    model=MyModel,
    field_config={
        'status': {
            'widget': 'select',
            'choices': [
                ('active', 'Active'),
                ('inactive', 'Inactive'),
                ('pending', 'Pending')
            ]
        },
        'priority': {
            'operators': ['equal', 'not_equal', 'greater', 'less']
        }
    }
)

Add Custom Filters

widget = QueryBuilderWidget(
    model=MyModel,
    extra_filters=[
        {
            'id': 'custom_field',
            'label': 'Custom Field',
            'type': 'string',
            'operators': ['equal', 'contains'],
            'input': 'text'
        }
    ]
)

Field Type Mapping

The widget automatically maps Django field types to QueryBuilder types:

Django Field QueryBuilder Type Default Operators
CharField, TextField, EmailField, URLField string equal, not_equal, contains, not_contains, begins_with, ends_with, is_null, is_not_null
IntegerField, BigIntegerField, PositiveIntegerField integer equal, not_equal, less, less_or_equal, greater, greater_or_equal, between, is_null, is_not_null
FloatField, DecimalField double equal, not_equal, less, less_or_equal, greater, greater_or_equal, between, is_null, is_not_null
BooleanField boolean equal
DateField date equal, not_equal, less, less_or_equal, greater, greater_or_equal, between, is_null, is_not_null
DateTimeField datetime equal, not_equal, less, less_or_equal, greater, greater_or_equal, between, is_null, is_not_null
TimeField time equal, not_equal, less, less_or_equal, greater, greater_or_equal, between
ForeignKey, OneToOneField integer equal, not_equal, less, less_or_equal, greater, greater_or_equal, between, is_null, is_not_null

Configuration Options

Widget Parameters

  • model: Django model class to generate filters from
  • fields: List of field names to include (default: all fields)
  • field_config: Dictionary of custom configurations per field
  • extra_filters: List of additional custom filters
  • attrs: Standard Django widget attributes

Field Configuration Options

Each field can be customized with:

  • widget: Input type ('text', 'textarea', 'select', 'radio', 'checkbox', 'number')
  • choices or values: List of tuples or dictionary for select options
  • operators: List of allowed operators for the field

Output Format

The widget stores queries as JSON in the following format:

{
  "condition": "AND",
  "rules": [
    {
      "id": "name",
      "field": "name",
      "type": "string",
      "input": "text",
      "operator": "contains",
      "value": "John"
    },
    {
      "condition": "OR",
      "rules": [
        {
          "id": "age",
          "field": "age",
          "type": "integer",
          "input": "number",
          "operator": "greater",
          "value": 25
        }
      ]
    }
  ]
}

Requirements

  • Python >= 3.8
  • Django >= 3.2

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

This widget integrates jQuery QueryBuilder by Damien Sorel.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

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

django_query_builder_widget-0.0.3.tar.gz (62.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_query_builder_widget-0.0.3-py3-none-any.whl (61.2 kB view details)

Uploaded Python 3

File details

Details for the file django_query_builder_widget-0.0.3.tar.gz.

File metadata

File hashes

Hashes for django_query_builder_widget-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e2cb2f1cc10fe079a269d36f93829b911ae34262f7dd30b3716f1b6afdd4e35c
MD5 933653c659cd5f8b13af6d9a52f4da25
BLAKE2b-256 a25ea4ba32e0645a22a8fb6710238dda188727bafd981eb8af81b79198105c5b

See more details on using hashes here.

File details

Details for the file django_query_builder_widget-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_query_builder_widget-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 388fff23614a6890e13c4d39fdfffc69254b30daf77b0ac8e41dda8e0d0bb039
MD5 3ae7406a7a0c22151309f344d2515cf4
BLAKE2b-256 8b9cc4e9d8a47e910a01b2ee7079bf5f35e5e9575c0ef9ef81216f0ef2e68c13

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