0

instance is not updated, using forms save () Django.

Could anyone help?

save() got an unexpected keyword argument 'cpnj'

views

cliente = Cliente.objects.get(user=request.user.id, pk=pk)
 if request.method == 'POST':
        form = ClienteForm(request.POST)
        if form.is_valid():
            form.update(instance=cliente, validated_data=form.cleaned_data)
            return redirect('clienteEdit', pk)

Forms

def update(self, instance, validated_data, context):
        print(instance)
        instance.save(**validated_data)

1 Answer 1

1

It looks like your model object Cliente and form ClientForm have a mismatch in one of the fields. Does your model object Cliente have a field called cpnj?

for updating I suggest you change the fields manually like this:

cliente = Cliente.objects.get(user=request.user.id, pk=pk)
 if request.method == 'POST':
        form = ClienteForm(request.POST)
        if form.is_valid():
            cliente.cpnj = form.cleaned_data['cpnj']
            cliente.save()
            return redirect('clienteEdit', pk)
Sign up to request clarification or add additional context in comments.

4 Comments

Yes. Yes. class ClienteForm(forms.Form): cpnj = forms.CharField()
class Cliente(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) cpnj = models.CharField(max_length=14)
I'm using forms.Form
i added an edit to my answer, see if that works, please note that form.cleaned_data['cpnj'] may not be correct since I dont know what its called inside the ClienteForm class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.