1 2 3 4 5 6 7 8 | from functools import partial
from django.db import models
class ChoiceFilterManager(models.Manager):
def __init__(self, field, choices, *args, **kwargs):
super(ChoiceFilterManager, self).__init__(*args, **kwargs)
for k,v in dict(choices).items():
setattr(self, v.lower(), partial(self.filter, **{field:k}))
|
More like this
- Login message middleware by nipuL 5 years, 1 month ago
- ModelMixin by eallik 2 years, 6 months ago
- CustomQueryManager by zvoase 4 years, 10 months ago
- Custom managers with chainable filters by itavor 5 years, 4 months ago
- Base64Field: base64 encoding field for storing binary data in Django TextFields by bikeshedder 3 years, 9 months ago
Comments
I think using a metaclass would be better than setattr in init().
#