The problem is: i select model records from db using
from_db = SimpleModel.objects.only('name', 'position').filter(...).exclude(...).order_by('position')
And I need to set new position for both of them. Coincidently, they're just switching their places.
So, I write:
for i, control in enumerate(movable_controls):
control.position = i
control.save()
And in debugger I see strange queries:
SELECT (1) AS "a"
FROM "tbl1"
WHERE "tbl1"."id" = 514 LIMIT 1;
SELECT "tbl1"."id",
"tbl1"."parent"
FROM "tbl1"
WHERE "tbl1"."id" = 514;
SELECT COUNT(*)
FROM "tbl1"
WHERE "tbl1"."parent" = 9;
UPDATE "tbl1"
SET "parent" = 9,
"position" = 0
WHERE "tbl1"."id" = 514
And the same for second element.
And what's stranger, now we have records with duplicate position in database, their positions are not recalculated according to these ones.
So, the queries are strange, and there is a problem in recalculation of positions.
Now, I used raw sql to fix this, but it does not look good :)
Hi. I've had a problem while emplementing subj.
The problem is: i select model records from db using from_db = SimpleModel.objects.only('name', 'position').filter(...).exclude(...).order_by('position') And I need to set new position for both of them. Coincidently, they're just switching their places. So, I write: for i, control in enumerate(movable_controls): control.position = i control.save() And in debugger I see strange queries:
And the same for second element. And what's stranger, now we have records with duplicate position in database, their positions are not recalculated according to these ones.
So, the queries are strange, and there is a problem in recalculation of positions.
Now, I used raw sql to fix this, but it does not look good :)
And also, thanks for quick fix of #17a1f50 :)