[Django]-Django,edit form with unique field, form.is_valid return false

4👍

You should pass the instance argument with the model instance which you want to edit:

from django.shortcuts import get_object_or_404

def viewA(request, pk):
    obj = get_object_or_404(modelA, pk=pk)
    if request.method == 'POST':
        form = formA(request.POST, instance=obj)
        if form.is_valid():
            ...

Leave a comment