Skip to content

Instantly share code, notes, and snippets.

@JonnyFunFun
Created March 19, 2014 16:01
Show Gist options
  • Save JonnyFunFun/9644949 to your computer and use it in GitHub Desktop.
Save JonnyFunFun/9644949 to your computer and use it in GitHub Desktop.
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