django-strategy-field
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
Metadata
Release files for django-strategy-field 3.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_strategy_field-3.2.1.tar.gz | 9.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_strategy_field-3.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.8 kB
Release files / django_strategy_field-3.2.1.tar.gz
| Download URL | django_strategy_field-3.2.1.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d87083f4a0ed7160ba2cff1b718bde670232ae557c6ec4c0a95d06b49419bc5b
|
|
BLAKE2b-256 checksum How to use checksums |
6b8698783fe60e8e016851c5f96727cb7ed4375aa9bb738b7e4cf6fbc8f3f287
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2025.
Transparency logRelease files / django_strategy_field-3.2.1-py3-none-any.whl
| Download URL | django_strategy_field-3.2.1-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fabdf06052fcea9fe319fd855fb2a308ec36ce513f1902688f369fbdbd00e1c6
|
|
BLAKE2b-256 checksum How to use checksums |
1502d0f2d8f69e619c4ab446571ad2b954b41f5f39a7ebc16cad927a3ca4f698
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2025.
Transparency log