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

This commit is contained in:
Peter Braun 2024-09-17 18:29:36 +02:00 committed by GitHub
parent eb4f3c2864
commit d2f3c02945
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()