Browse files

Enhanced RandomHashField, added hash_length option. Fixed annoying De…

  • Loading branch information...
1 parent 709e95b commit 1673e0ef1e8545e9b8f53dc5a5e3ca4e5ce572ec @dfryta dfryta committed Aug 3, 2011
Showing with 6 additions and 4 deletions.
  1. +6 −4 django_common/
10 django_common/
@@ -31,7 +31,7 @@ def to_python(self, value):
return value
- def get_db_prep_save(self, value):
+ def get_db_prep_save(self, value, connection=None):
"""Convert our JSON object to a string before we save"""
if value == "":
@@ -116,16 +116,17 @@ class RandomHashField(fields.CharField):
@param update_on_save optional field whether to update this hash or not, everytime the model instance is saved
- def __init__(self, update_on_save=False, *args, **kwargs):
+ def __init__(self, update_on_save=False, hash_length=None, *args, **kwargs):
#TODO: args & kwargs serve no purpose but to make django evolution to work
self.update_on_save = update_on_save
- super(fields.CharField, self).__init__(max_length=128, unique=True, blank=False, null=False, db_index=True, default=md5_hash())
+ self.hash_length = hash_length
+ super(fields.CharField, self).__init__(max_length=128, unique=True, blank=False, null=False, db_index=True, default=md5_hash(max_length=self.hash_length))
Tivix member
chachra added a line comment Aug 5, 2011

looks good .... except why can't we use max_length parameter itself ?

I guess going with 128 gives us flexibility in terms of increasing hash_length in future with no db impact. hmm... but seems non-optimal otherwise?

Tivix member
dfryta added a line comment Aug 8, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
def pre_save(self, model_instance, add):
if not add and not self.update_on_save:
return getattr(model_instance,
- random_hash = md5_hash()
+ random_hash = md5_hash(max_length=self.hash_length)
setattr(model_instance,, random_hash)
return random_hash
@@ -135,6 +136,7 @@ def pre_save(self, model_instance, add):
[], # Positional arguments (not used)
{ # Keyword argument
"update_on_save": ["update_on_save", {"default": False}],
+ "hash_length": ["hash_length", {"default": None}],
], ["^django_common\.db_fields\.RandomHashField"])

0 comments on commit 1673e0e

Please sign in to comment.