dj-flexi-tagο
A flexible and efficient tagging system for Django models with service-only architecture.
Overviewο
Django Flexi Tag is a powerful tagging solution for Django projects built with a service-only architecture for maximum compatibility and composability. Unlike traditional tagging libraries that rely on complex many-to-many relationships, dj-flexi-tag uses PostgreSQLβs native JSON capabilities and a clean service layer for efficient and flexible tag management.
Key Featuresο
π Service-Only Architecture: Clean and composable design with full QuerySet compatibility
π Composable Filtering: Preserves existing QuerySet filters when adding tag filtering
β‘ Easy Integration: Works seamlessly with Django REST Framework ViewSets
π¦ Flexible Tag Storage: Uses PostgreSQL JSONField for efficient and flexible tag storage
π€ Automatic Model Generation: Generates auxiliary Tag models for your existing models
π Bulk Operations: Support for bulk tag operations on multiple objects
π― Custom Exception Integration: Configurable base exception classes for seamless project integration
π Django Compatibility: Works across multiple Django versions (1.11 to 5.0)
π Python Compatibility: Supports Python 3.5+
Quick Startο
Install the package:
pip install dj-flexi-tag
Add to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'flexi_tag',
# ...
]
Make your model taggable:
from flexi_tag.utils.models import FlexiTagMixin
class Product(FlexiTagMixin):
name = models.CharField(max_length=100)
# other fields...
Generate tag models:
python manage.py generate_tag_models
Create and apply migrations:
python manage.py makemigrations
python manage.py migrate
Use the service to add tags:
from flexi_tag.utils.service import TaggableService
# Add tags to instances
service = TaggableService()
service.add_tag(product, 'featured')
service.bulk_add_tags(product, ['sale', 'new-arrival'])
# Filter QuerySets by tags - preserves existing filters!
featured_products = service.filter_by_tag(Product.objects.filter(is_active=True), 'featured')
Add tagging to your ViewSet (optional for REST API):
from rest_framework import viewsets
from flexi_tag.utils.views import TaggableViewSetMixin
class ProductViewSet(viewsets.ModelViewSet, TaggableViewSetMixin):
queryset = Product.objects.all()
serializer_class = ProductSerializer
Now you can use the tagging API to add, remove, and manage tags on your models!
API Usage Exampleο
Add a tag to an object:
POST /api/products/1/add_tag/
{"key": "featured"}
Add multiple tags:
POST /api/products/1/bulk_add_tag/
{"keys": ["sale", "new-arrival"]}
Remove a tag:
POST /api/products/1/remove_tag/
{"key": "featured"}