<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_object_data(obj, fields, safe):
 """ Given an object and a list of fields, recursively build an object for serialization. Returns a dictionary. """


    temp_dict = dict()
    for field in fields:

        try:
            attribute = getattr(obj, str(field))
            if isinstance(attribute, list) and all([isinstance(item, models.Model) for item in attribute]):
                temp_dict[field] = []
                for item in attribute:
                    temp_dict[field].append(get_object_data(item, get_fields(item), safe))  # Recur
            elif isinstance(attribute, models.Model):
                attribute_fields = get_fields(attribute)
                object_data = get_object_data(attribute, attribute_fields, safe)  # Recur
                temp_dict[field] = object_data
            else:
                if not safe:
                    if isinstance(attribute, basestring):
                        attribute = cgi.escape(attribute)
                temp_dict[field] = attribute

        except Exception as e:
            logger.info("Unable to get attribute.")
            logger.error(e)
            continue

    return temp_dict