I have this design:

try: turn_ip = TurnIp.get(TurnIp.turn == turn.id) except TurnIp.DoesNotExist: turn_ip = None 

Is it possible to replace it with something like get_or_None , so that in case of a failed search, I’ll return None , and not an error?

    1 answer 1

    You can simply wrap this into a function:

     def get_or_None(turn, TurnIp): try: turn_ip = TurnIp.get(TurnIp.turn == turn.id) return turn_ip except TurnIp.DoesNotExist: return None