This piece of code is doing what I need it to do but I'm still not happy with it. Here's the main logic to it:Iterating over a dictionary, I want to treat differently the one item that has the key 'Companion', which has a dictionary as value. I'm also preventing the possibility for other values to be dictionaries/lists.This is an expectable input:{    FolioNumber: 125,    ReservationName: Manuel Manuel,    RoomID: 23,    RoomTypeID: E,    RealRoomTypeID: E,    ArrivalDate: 2016-09-20T00:00:00,    Nights: 27,    DepartureDate: 2016-10-17T00:00:00,    Adults: 2,    Children: 0,    ChildrenNoPay: 0,    Babies: 0,    Address1: null,    Address2: null,    ZipCode: null,    AddressCountry: {        CountryID: 41,        Country: Res.Portugal-Portugueses,        CountryISO3166a2: PT    },    TaxPayNumber: null,    Phone: null,    MobilePhone: null,    EmailAddress: null,    IDNumber: null,    IDType: P,    IDEmissionPlace: null,    BirthPlace: null,    Nationality: {        NationalityID: 41,        Country: Res.Portugal-Portugueses,        CountryISO3166a2: PT    },    Gender: null,    Idiom: null,    IsAttempt: 0,    FlightArrivalNumber: null,    FlightDepartureNumber: null,    Companion: {        ReservationCompanionDetailID: 170,        ReservationName: Pedro Pedro,        Address1: null,        Address2: null,        ZipCode: null,        AddressCountry: {            CountryID: 41,            Country: Res.Portugal-Portugueses,            CountryISO3166a2: PT        },        TaxPayNumber: null,        Phone: null,        MobilePhone: null,        EmailAddress: null,        IDNumber: null,        IDType: P,        IDEmissionPlace: null,        BirthPlace: null,        Nationality: {        NationalityID: 41,        Country: Res.Portugal-Portugueses,        CountryISO3166a2: PT        }    }}Here's a snippet:def generate_user_form_fields(data, api_form, pre_survey):    # data is a dict() object    for key, value in data.viewitems():        if type(value) is OrderedDict:        # Creates sub form only if key is 'Companion' and sets the 'Companion field into a node field        if key == 'Companion':            tmp_user_form = create_sub_form_instance(pre_survey, guest_form, date_now)            for sub_key, sub_value in value.viewitems():                node_field = ApiFormField.objects.filter(node_child_name=key).first()                # TMP Fix                if node_field is None:                    node_field = create_node_field(api_form, key + 's', key)                if type(sub_value) is OrderedDict:                    for sub_key2, sub_value2 in sub_value.viewitems():                        create_user_field(api_form, tmp_user_form, sub_key2, sub_value2,                                          data[api_form.compare_field], date_now, node_field=node_field)                else:                    create_user_field(api_form, tmp_user_form, sub_key, sub_value,                                      data[api_form.compare_field], date_now, node_field=node_field)        else:            for sub_key, sub_value in value.viewitems():                create_user_field(api_form, guest_form, sub_key, sub_value, data[api_form.compare_field], date_now)    elif type(value) is list:        for lst_item in value:            # Create sub form            tmp_user_form = create_sub_form_instance(pre_survey, guest_form, date_now)            for sub_lst_key, sub_lst_value in lst_item.viewitems():                node_field, = ApiFormField.objects.filter(node_child_name=key)                # TMP Fix                if node_field is None:                    node_field = create_node_field(api_form, key + 's', key)                if type(sub_lst_value) is OrderedDict:                    for sub_key2, sub_value2 in sub_lst_value.viewitems():                        create_user_field(api_form, tmp_user_form, sub_key2, sub_value2,                                          data[api_form.compare_field], date_now, node_field=node_field)                else:                    create_user_field(api_form, tmp_user_form, sub_lst_key, sub_lst_value,                                      data[api_form.compare_field], date_now, node_field=node_field)    else:        create_user_field(api_form, guest_form, key, value, data[api_form.compare_field], date_now)return TrueAnd this is my create_user_field() function:def create_user_field(api_form, user_form, key, value, compare_field, create_date, node_field=None):    if node_field is None:        api_form_field = ApiFormField.objects.filter(import_field_name=key).first()    else:        api_form_field = ApiFormField.objects.filter(import_field_name=key).filter(api_form_field=node_field.id).first()    if api_form_field is None:        api_form_field = create_api_field(api_form, key, node_field)    if value is None:        value = ''    user_field = UserFormField()    user_field.user_form_id = user_form.id    user_field.api_form = api_form    user_field.api_form_field = api_form_field    user_field.original_value = value    user_field.last_value = value    user_field.title = key    user_field.compare_field = compare_field    user_field.create_date = create_date    user_field.edit_date = create_date    user_field.save()    return TrueAny suggestions to this still-novice developer?
Generate user form fields based on a dictionary that may have nested dictionaries
python;beginner;form;dictionary