Hi everyone. I am new to Django. I have a model which stores people and a one to one relationship to another table which stores details if the person is a visitor to our organization.
I have a form to enter the person and the details of their visit using an inline form. Usually a visitors first visit will be their first interaction with our organization and the first time that person is saved in the database, but they might already have interacted with us and joined our mailing list before coming to visit. Currently when the user enters the existing person name and surname in the form if that name, surname combination exists in the form they get a message saying that the person already exists and the form is repopulated. I would like to redirect to another form where they can select from existing Persons in the database if this is the case and then if there is a different error on the form to have the usual redirect to display the form again. Is there a way to do this?
You’re already catching the condition of a duplicate name, so if that condition occurs, the view can return a redirect to a different view that displays your desired form to select the existing name.
Keep in mind that just printing something returns a string representation of an object - it may hide the actual structure of the underlying data.
In this situation, you have access to the form.errors object. If you read the information about that object and investigate the type of data it contains, you’ll see how you can make use of it for your needs.