1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from functools import wraps
def unique_boolean(field):
def factory(func):
@wraps(func)
def decorator(self):
if getattr(self, field):
try:
tmp = self.__class__.objects.get(**{ field: True })
if self != tmp:
setattr(tmp, field, False)
tmp.save()
except self.__class__.DoesNotExist:
pass
return func(self)
return decorator
return factory
# Usage:
class MyDefaultUniqueModel(models.Model):
default = models.BooleanField()
@unique_boolean('default')
def save(self):
super(MyDefaultUniqueModel, self).save()
|
More like this
- True Unique Boolean Model Decorator by kunitoki 1 year ago
- TrueNoneField by diverman 3 years, 5 months ago
- RequiredNullBooleanField by wwu.housing 4 years, 2 months ago
- CustomQueryManager by zvoase 4 years, 10 months ago
- Unique FileFiled or FileFiled with custom validation and overwriting files on update by evilclay 2 years, 4 months ago
Comments