Unpickle a model by retrieving it from the database.

def _model_unpickle(cls, data):
    """Unpickle a model by retrieving it from the database."""
    auto_field_value = data['pk']
    try:
        obj = cls.objects.get(pk=auto_field_value)
    except Exception as e:
        if isinstance(e, OperationalError):
            # Attempt reconnect, we've probably hit;
            # OperationalError(2006, 'MySQL server has gone away')
            logger.debug("Caught OperationalError, closing database connection.", exc_info=e)
            from django.db import connection
            connection.close()
            obj = cls.objects.get(pk=auto_field_value)
        else:
            raise
    return obj