From 70dbeca5de5c5d383990ad859d13db43df7fdf92 Mon Sep 17 00:00:00 2001 From: Jan Rydzewski Date: Fri, 15 Apr 2022 13:23:42 +0200 Subject: [PATCH] Sort in-memory object list after .create() This is required when newly created object is ordered not at the end. --- modelcluster/fields.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modelcluster/fields.py b/modelcluster/fields.py index 3542991..e8f9558 100644 --- a/modelcluster/fields.py +++ b/modelcluster/fields.py @@ -160,6 +160,11 @@ def create(self, **kwargs): items = self.get_object_list() new_item = related.related_model(**kwargs) items.append(new_item) + + # Sort list + if rel_model._meta.ordering: + sort_by_fields(items, rel_model._meta.ordering) + return new_item def clear(self):