Skip to main content

Advanced search syntax for Django Admin

Project description

Django Admin Advanced Search

PyPI License Python Version

Advanced search syntax for Django Admin that enables powerful filtering capabilities for text fields directly from the search bar.

中文版 说明

Features

  • Enhanced search capabilities in Django Admin with advanced syntax for text fields
  • Support for field-specific searches with various operators
  • Case-sensitive and case-insensitive matching options
  • Multiple conditions combination with AND logic
  • Seamless integration with existing Django Admin interfaces
  • Performance-conscious implementation with database-level filtering

Requirements

  • Python >= 3.12
  • Django >= 5.1

Note: This package has been tested specifically on Django 5.1, 5.2 with Python 3.12. While it may work on other versions, compatibility is not guaranteed for versions outside this range.

This package is designed specifically for text field searches. For numeric and date/time field filtering, consider using django-admin-rangefilter.

Installation

pip install django-admin-advanced-search

Usage

  1. Add django_admin_advanced_search to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'django_admin_advanced_search',
    ...
]
  1. Use the advanced search in your admin classes:
from django.contrib import admin
from django_admin_advanced_search.mixins import AdvancedSearchMixin

class MyModelAdmin(AdvancedSearchMixin, admin.ModelAdmin):
    search_fields = ['name', 'description', 'author__name']  # Fields that can be searched
    # Your other admin configuration

admin.site.register(MyModel, MyModelAdmin)

Search Syntax

Syntax Description Example SQL Equivalent
field:value Case-insensitive contains name:john name ILIKE '%john%'
field:=value Case-insensitive exact name:=john name ILIKE 'john'
field:==value Case-sensitive exact name:==John name = 'John'
field:!value Case-sensitive contains name:!john name LIKE '%john%'
field:*suffix Case-insensitive endswith name:*son name ILIKE '%son'
field:!*suffix Case-sensitive endswith name:!*son name LIKE '%son'
field:prefix* Case-insensitive startswith name:john* name ILIKE 'john%'
field:!prefix* Case-sensitive startswith name:!john* name LIKE 'john%'

Combined Search

Advanced search syntax can be combined with plain text search. When both are present, the advanced search filters are applied first, then the plain text search is applied to the filtered results using Django's default search behavior.

Examples:

  • title:=Python lisa - Find items with title exactly matching "Python" (case-insensitive) AND containing "lisa" in any search field
  • lisa title:=Python - Same as above, order doesn't matter
  • title:django* tutorial - Find items with titles starting with "django" (case-insensitive) AND containing "tutorial" in any search field

Examples

  • title:django* - Items with titles starting with "django" (case-insensitive)
  • author__name:*smith - Items by authors whose names end with "smith" (case-insensitive)
  • title:==Learning Python - Items with the exact title "Learning Python" (case-sensitive)
  • title:"Python Programming" - Items with titles containing the exact phrase "Python Programming"
  • title:python author__name:john - Items with titles containing "python" AND authors whose names contain "john"
  • title:=Python lisa - Items with titles exactly matching "Python" (case-insensitive) AND containing "lisa" in any search field

Testing

This package uses Django's built-in testing framework. To run the tests:

python -m django test tests --settings=tests.settings

Database Collation and Performance Notes

  • This package generates database-level filters, ensuring optimal performance
  • For case-insensitive operations, the package uses the database's native case-insensitive comparison operators when available
  • Make sure your database columns have appropriate collation settings for optimal performance
  • Complex searches with multiple fields may generate complex SQL queries; consider adding database indexes on frequently searched fields
  • When using related field lookups (e.g., author__name:john), ensure foreign key relationships are properly indexed

Contributing

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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

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

Project Link: https://github.com/shifenhutu/django-admin-advanced-search

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_admin_advanced_search-0.1.4.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

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

django_admin_advanced_search-0.1.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_advanced_search-0.1.4.tar.gz.

File metadata

File hashes

Hashes for django_admin_advanced_search-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f5da0c469ce6d73340a12e2c6c89467dc9d9d11246778cd9726d2b464403c3f7
MD5 6e5163c5885c7d42e64bd3860c6c6e9d
BLAKE2b-256 43ac6882371560479823212afd3b2aab9eb30e86807b86184944fc078e66f3f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_advanced_search-0.1.4.tar.gz:

Publisher: publish.yml on shifenhutu/django-admin-advanced-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_admin_advanced_search-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_advanced_search-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 22f54a4b157da10249e7b7f26b7dd53ff10f20ec4b0daa5c561a7969e87e2b91
MD5 3d1219af2e4cde93e71a1bbb335418ef
BLAKE2b-256 7cda8bf76af669fee2a7e21ca411fafbea88256199386948adcbe39905caa0f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_advanced_search-0.1.4-py3-none-any.whl:

Publisher: publish.yml on shifenhutu/django-admin-advanced-search

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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