fix: maintain order of insertions into m2m relationship tables (#15536)

This commit is contained in:
Peter Braun 2024-09-17 13:37:35 +02:00 committed by GitHub
parent ef8cb892cb
commit af900c8370
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1039,7 +1039,7 @@ class OrderedManyToManyField(models.ManyToManyField):
descriptor = getattr(instance, self.name)
order_with_respect_to = descriptor.source_field_name
for i, ig in enumerate(sender.objects.filter(**{order_with_respect_to: instance.pk})):
for i, ig in enumerate(sender.objects.filter(**{order_with_respect_to: instance.pk}).order_by('id')):
if ig.position != i:
ig.position = i
ig.save()