Created
March 19, 2014 16:01
-
-
Save JonnyFunFun/9644949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from django.db.models.signals import pre_save | |
def full_clean_on_save(cls): | |
""" | |
Class decorator that automatically calls full_clean() when a model is saved. | |
""" | |
def connect(signal, func): | |
cls.func = staticmethod(func) | |
@wraps(func) | |
def wrapper(sender, **kwargs): | |
return func(kwargs.get('instance')) | |
signal.connect(wrapper, sender=cls) | |
return wrapper | |
connect(pre_save, cls.full_clean) | |
return cls | |
#--------- | |
# Example usage | |
@full_clean_on_save | |
class MyModel(models.Model): | |
tits = CharField(max_length=5, default="(.Y.)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment