I need to check if an object exists in a table. If the object doesn't exist I want to save it, if it does exist I want to refresh the page and tell the user that the identifier (quote number) already exists. I have this example code:
def some_method(request):
if request.method == 'POST':
form = SomeForm(request.POST)
if form.is_valid:
quote = request.POST.get('quote')
if SomeModel.objects.get(quote_number = quote).exists():
refresh with error #not sure how to do
else:
save object #I can do this part
The problem I am running into is that when I check if the object exists (and it does) then the code will raise an error saying the object doesn't exist before it hits the if. Which means on the webpage is full of coding information rather than the refresh with a message for the user.
I want to be able to have a little pop up message or something for the user to enter a new quote number rather than having the developer error page show up.
Am I going about this the correct way?
SomeForminvolve the quote at all? ideally that shouldn't be valid if it really isn'tSomeFormis a form for posting information about a quote. It's considered "valid" if all the fields are entered correctly. I need to check before the information is save, if the quote number (which is the unique identifier) has already been put into the table.