N
N
NyxDeveloper2021-01-21 18:50:14
Django
NyxDeveloper, 2021-01-21 18:50:14

How to edit an object with a unique field using a form?

The problem appeared out of nowhere. It is necessary to implement the object editing function through the form, to which the instance is passed to fill in the fields. Django complains that the form is not valid and refuses to accept it. How to make the form pass validation if it is editing, and when creating a new object, it also checks for uniqueness? I just don't really want to remove form.is_valid() somehow.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Belokurov, 2021-01-22
@NyxDeveloper

Make sure that when the form is submitted, the pk is also submitted. Django distinguishes between creating and editing based on it. If pk is not passed on submission, Django considers this to be the creation of a new object, and then you will already get an error due to a non-unique field.

D
Dr. Bacon, 2021-01-21
@bacon

The problem appeared out of nowhere.
No, it was you who somehow created it, in a normal situation it does not exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question