Django custom field to implement the strategy pattern
Project description
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 links
Stable |
||||
Development |
||||
Project home page: |
||||
Issue tracker: |
||||
Download: |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-strategy-field-2.0.tar.gz
.
File metadata
- Download URL: django-strategy-field-2.0.tar.gz
- Upload date:
- Size: 25.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fafd78f9d75a1edcfba2f9cae8f8f1b8609ebd9ec84c310f5eea6fc35ba21a9b |
|
MD5 | 4ac5be4e5d56dd275532bd9d5a4de442 |
|
BLAKE2b-256 | 191679f4be9583f64f760d16002cea85e3f37b0a7a91c9c469f690da636f017e |