Skip to main content

Add tags to any model in Django via ModelViewSet

Project description

Django Flexi Tag

Build status Documentation status PyPI PyPI - Django version PyPI - Python version PyPI - License

A flexible and efficient tagging system for Django models that allows you to add, remove, and manage tags on any Django model with minimal configuration.

Features

  • 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
  • Django Compatibility: Works across multiple Django versions (1.11 to 5.0)
  • Python Compatibility: Supports Python 3.5+

Installation

Installation using pip:

pip install dj-flexi-tag

For development and testing:

pip install dj-flexi-tag[dev,test]

For documentation development:

pip install dj-flexi-tag[docs]

For all dependencies:

pip install dj-flexi-tag[dev,test,docs]

Add flexi_tag to your INSTALLED_APPS and run migrations:

INSTALLED_APPS = (
    # other apps here...
    'flexi_tag',
)

Testing

Run the tests with:

python runtests.py

Use the --verbose flag for more detailed output:

python runtests.py --verbose

Use the --interactive flag for interactive mode:

python runtests.py --interactive

Basic Usage

1. Define Your Models with FlexiTagMixin

from flexi_tag.utils.models import FlexiTagMixin

class YourModel(FlexiTagMixin):
    name = models.CharField(max_length=100)
    # other fields...

2. Generate Tag Models

Run the management command:

# Standard usage
python manage.py generate_tag_models

# For testing what would be generated
python manage.py generate_tag_models --dry-run

# If models aren't detected after recent changes (force reload)
python manage.py generate_tag_models --force-reload

This will create a flexi_generated_model.py file in the same directory as your model, which contains a YourModelTag model. The command will also automatically add the appropriate import statement to your models.py file and run makemigrations to generate migration files for the new models.

from .flexi_generated_model import YourModelTag  # noqa

If multiple tag models are generated, they will be imported in a single line:

from .flexi_generated_model import FirstModelTag, SecondModelTag  # noqa

3. Apply Migrations

After the tag models have been generated and migrations created, apply them with:

python manage.py migrate

4. Add Tagging to Your ViewSet

from rest_framework import viewsets
from flexi_tag.utils.views import TaggableViewSetMixin
from .models import YourModel

class YourModelViewSet(viewsets.ModelViewSet, TaggableViewSetMixin):
    queryset = YourModel.objects.all()
    serializer_class = YourModelSerializer

5. Use the Tagging API

Add a tag:

POST /your-model/1/add_tag/
{"key": "important"}

Add multiple tags:

POST /your-model/1/bulk_add_tag/
{"keys": ["important", "urgent"]}

Remove a tag:

POST /your-model/1/remove_tag/
{"key": "important"}

Advanced Usage

For more advanced usage, including programmatic tag management, custom tag validation, and more detailed configuration options, please refer to the documentation.

Contributing

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

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

dj-flexi-tag-1.1.3a4.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

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

dj_flexi_tag-1.1.3a4-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file dj-flexi-tag-1.1.3a4.tar.gz.

File metadata

  • Download URL: dj-flexi-tag-1.1.3a4.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for dj-flexi-tag-1.1.3a4.tar.gz
Algorithm Hash digest
SHA256 b37ba51096fcb744f0640e632d2124d9cf2754361ffd2a5081fb7f9028e8d2cd
MD5 0c1456b5fad1bdb40a29887453e0c0e6
BLAKE2b-256 e43dc9c4305a7133bc9fcf5da89d06af636339293865bcb9968b56f66ee1557c

See more details on using hashes here.

File details

Details for the file dj_flexi_tag-1.1.3a4-py3-none-any.whl.

File metadata

  • Download URL: dj_flexi_tag-1.1.3a4-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for dj_flexi_tag-1.1.3a4-py3-none-any.whl
Algorithm Hash digest
SHA256 8e3d85a923022c0a087f447d1dc8ce37331a64f93e25e51b5e6006081be61189
MD5 7ebfe6ecf285eac5ef5c9253871819f1
BLAKE2b-256 faa6b64101333cfc8a25a9a0b012b244c161cedcbd475ac5785325766dd2949a

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