def add_rsvps(request): results = request.FILES['file'].get_array(sheet_name=None, name_columns_by_row=0) dictionary = {} for idx, row in enumerate(results): if idx == 0: for col_idx, column_name in enumerate(row): dictionary[column_name] = col_idx else: new_row = _special_initialize(row, dictionary) user_values = { "first_name": new_row[dictionary["First Name"]], "last_name": new_row[dictionary["Last Name"]], "email": new_row[dictionary["Email Address"]], "password": "******" } rsvp_values = { "plus_one": new_row[dictionary["Plus One"]], "formal_prefix": new_row[dictionary["Formal"]], "affiliation": new_row[dictionary["Affiliation"]], "expected_attendees": new_row[dictionary["RSVP"]], } user = User.objects.filter(first_name=user_values["first_name"], last_name=user_values["last_name"]) if user.exists(): user = User.objects.get(first_name=user_values["first_name"], last_name=user_values["last_name"]) user.email = user_values["email"] user.save() else: user_values["username"] = create_random_string() user = User.objects.create_user(**user_values) try: user.rsvp.edit(rsvp_values) except: user.rsvp = RSVP(**rsvp_values) user.rsvp.save() return HttpResponse("Success", status=200)