Django Builds Integrity for Parallel Work on Unique Fields

I noticed that I do not know if this is a real problem, or I am doing the wrong design.

I have a model:

class A(Model): name = CharField(unique=True, max_length=255) 

and the model form associated with A, this modelform has a clean_name () method that checks if this field is unique in db (ignore the fact that modelforms already do this by default, I indicate that for example here).

In the view, if I do

 o = form.save(commit=False) # xyz o.save() 

and in #xyz I have another client that inserts an object A with the same value of the name field, o.save() Integrityerror exception, correctly preventing the duplicate entry from being inserted.

What do I need to know how to handle these cases, should I wrap this o.save() with a try / except block and fill in the error field in the form that determines the choice of another name value?

This is a fairly common case that should happen to everyone, and this decision is terrible, so I think I'm doing something terribly wrong.

+6
source share
1 answer

I suspect it could be like this:

Model forms provide uniqueness checking only when the flag is set to xx_clean() . If you override clean with your own (like yours), you need to call the clean() superclass. See rewriting a clean method .

0
source

Source: https://habr.com/ru/post/969711/


All Articles