+ def save(self, *args, **kwargs):
+ if not self.key:
+ self.key = self.generate_key()
+ return super(Contact, self).save(*args, **kwargs)
+
+ @classmethod
+ def generate_key(cls):
+ key = ''
+ while not key or key in [record['key'] for record in cls.objects.values('key')]:
+ key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30))
+ return key
+