Skip to main content

custom fields useful to implement the Strategy Pattern with Django models.

Project description

django-strategy-field

Pypi coverage Test Django

Set of custom fields useful to implement the Strategy Pattern with Django models.

The Strategies are displayed in SelectBoxes as standard choice field

This package provides the following custom fields:

  • StrategyField
  • MultipleStrategyField
  • StrategyClassField
  • MultipleStrategyClassField

The StrategyField can be accessed as instance of the model with an attribute context that points to model that 'owns' the field (inverse relation). So:

Example

    from strategy_field.fields import StrategyField
    from django.core.mail.backends.filebased.EmailBackend


    class Event(models.Model):
        backend = StrategyField()

    Event(sender='django.core.mail.backends.filebased.EmailBackend')

Use case

As example we can imagine an application that manages Events that need to be notified to users. Each Occurrence of Event can be notified using different transport, (email, sms,...). We want to be able to add/change the way we send notification, per event basis, simply using the Django admin panel.

    from strategy_field.fields import StrategyField
    from strategy_field.registry import Registry

    class TransportRegistry(Registry)
        pass

    class AbstractStrategy(object):
        def __init__(self, context):
            self.context = context

        def send(self):
            raise NotImplementedError

    class EmailStrategy(AbstractTransport):
        def send(self):
            ...

    class SMSStrategy(AbstractTransport):
        def send(self):
            ...
    registry = TransportRegistry(AbstractStrategy)
    registry.register(EmailStrategy)
    registry.register(SMSStrategy)

    class Event(models.Model):
        sender = StrategyField(registry=registry)

    Event.objects.get_or_create(sender=EmailStrategy)
    ...
    ...
    e = Event.objects.get(sender=EmailStrategy)
    e.sender.send() # e.sender.context == e

More examples

Use callable

    from strategy_field.fields import StrategyField
    from strategy_field.registry import Registry

    registry1 = Registry()
    registry2 = Registry()

    class A(model):
        sender = StrategyField(registry=lambda model: model._registry)
        class Meta:
            abstract = True

    class C1(A):
        _registry = registry1
        class Meta:
            abstract = True

    class C2(A):
        _registry = registry2
        class Meta:
            abstract = True

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_strategy_field-3.2.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

django_strategy_field-3.2.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file django_strategy_field-3.2.1.tar.gz.

File metadata

  • Download URL: django_strategy_field-3.2.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for django_strategy_field-3.2.1.tar.gz
Algorithm Hash digest
SHA256 d87083f4a0ed7160ba2cff1b718bde670232ae557c6ec4c0a95d06b49419bc5b
MD5 a01fcf49af8d915e838b5ae54f63947c
BLAKE2b-256 6b8698783fe60e8e016851c5f96727cb7ed4375aa9bb738b7e4cf6fbc8f3f287

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_strategy_field-3.2.1.tar.gz:

Publisher: release.yml on saxix/django-strategy-field

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_strategy_field-3.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_strategy_field-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fabdf06052fcea9fe319fd855fb2a308ec36ce513f1902688f369fbdbd00e1c6
MD5 fff869fd69735624f05c6fbee82ec1ec
BLAKE2b-256 1502d0f2d8f69e619c4ab446571ad2b954b41f5f39a7ebc16cad927a3ca4f698

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_strategy_field-3.2.1-py3-none-any.whl:

Publisher: release.yml on saxix/django-strategy-field

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