Ajay Chaurasia
Ajay Chaurasia

Reputation: 119

unique=True gives already exist! Even if interchanging the values of two objects

To display objects (members) in particular order, I have made a field, order:

order = models.IntegerField(unique=True,null=True,blank=True)

so that I can do an .order_by('order') to get the required order.

In Django Admin, the table has only two objects with order 0,1. If I want to interchange it by 1,0 I get an error:

About us with this Order already exists

Enter image description here

For ease of using Django admin, is there a better method to achieve the above?

Upvotes: 0

Views: 141

Answers (1)

mrash
mrash

Reputation: 903

You can remove unique=True and add a custom action to reorder objects. In this form, Django admin applies each object in a separate transaction and it causes this error. You may override the update function of your admin class and do all changes in a bulk-update transaction like this. But I don't recommend it. Because you may make a mistake in the future and want to edit other fields and this line makes a bug.

Upvotes: 2

Related Questions