How to save form to database only if its not already in the database in django

Viewed 67

Here is the code and Im wondering how can I make it so it saves if the input from the form is unique and not already in the db.

@login_required    
def settings(request):
    form = EmailInfoForm()
    
    if request.method == "POST":
        
        form = EmailInfoForm(request.POST)
        
        if form.is_valid():
            form.save()
    
    
    context = {'form': form}
    return render(request, 'tasks/settings.html', context)
1 Answers

By looking at form name, it seems you want unique email, so by default EmailField is not unique, you can add unique=True to make it unique, see unique option.

Related