0
votes

I'm creating a app that get information from some users, like name, email, ID number, etc. I want to get a response from a POST but in some cases i don't get the email from the users, so I need to create a parameter with this empty data.

In other words I'm making a parse of the parameters of the POST and checking if all the data is good and legit, if I don't get the email from the API i want to create a parameter or maybe a empty string at least.

            try:
                if not email:
                    errors['email'] = _(u'This data is missing.')
                else:
                    validate_email(email)
                    if email != user_data['email']:
                        if user_data['email'] in ["", None]:
                            user_data['email'] = email
                        # If the email is modified, its no longer verified and I check if the user is registered with another email.
                        if 'email_verified' in user_data:
                            user_data['email_verified'] = False
                        users_same_mail = User.objects.filter(email=email)
                        if users_same_mail.exists():
                            # I check if the email is twice registered
                            # I give the user another email registered in the database
                            errors['email'] = _(
                                u'The email is already registered.')

            except ValidationError as e:
                errors['email'] = _(
                    u'Bad email, please enter another one.')```

What can i write to create a empty string or some value, all i want is to set email to something that is not Null because it will raise a KeyError later when i try to access that key. 
1

1 Answers

0
votes

Already solved.

I add the key by creating an if.

If 'email' not in user_data.keys(): user_data['email'] = ''

By adding this if there is any KeyError it will add the empty string.