Fix transaction breakage in Django 1.6

AttributeError: 'NoneType' object has no attribute 'startswith'
sid happens to be None in one or more cases, just test it's not None before returning it.