class EncryptedPKModelManager(models.Manager):
    """Allows models to be identified based on their encrypted_pk value."""

def get(self, *args, **kwargs):
    encrypted_pk = kwargs.pop('encrypted_pk', None)
if encrypted_pk:
    kwargs['pk'] = struct.unpack('<Q', self.model.encryption.decrypt(struct
        .pack('<Q', encrypted_pk)))[0]
return super(EncryptedPKModelManager, self).get(*args, **kwargs)


class EncryptedPKModel(models.Model):
    """Adds encrypted_pk property to children."""
encryption = DES.new('8charkey')

def _encrypted_pk(self):
    return struct.unpack('<Q', self.encryption_obj.encrypt(str(struct.pack(
        '<Q', self.pk))))[0]
encrypted_pk = property(_encrypted_pk)


class Meta:
    abstract = True
Creating django objects with a random primary key