Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

GroupedModelSelect: Last group not shown occasionally #14

Open
runekaagaard opened this Issue · 1 comment

2 participants

@runekaagaard

When the last group only have one item, the last group is not shown. This patch fixes that problem in a very procedural way. Could it find its way upstream?

Index: libs/smart_selects/form_fields.py
===================================================================
--- libs/smart_selects/form_fields.py   (revision 6379)
+++ libs/smart_selects/form_fields.py   (working copy)
@@ -40,16 +40,20 @@
         # accessed) so that we can ensure the QuerySet has not been consumed. This
         # construct might look complicated but it allows for lazy evaluation of
         # the queryset.
-        final = [("", self.empty_label or "---------"), ]
-        group = None
+        group_indexes = {}
+        choices = [("", self.empty_label or "---------")]
+        i = len(choices)
         for item in self.queryset:
-            if not group or group[0] != unicode(getattr(item, self.order_field)):
-                if group:
-                    final.append(group)
-                group = [unicode(getattr(item, self.order_field)), []]
-            group[1].append(self.make_choice(item))
-        return final
-
+            order_field = getattr(item, self.order_field)
+            group_index = order_field.pk
+            if not group_index in group_indexes:
+                group_indexes[group_index] = i
+                choices.append([unicode(order_field), []])
+                i += 1
+            choice_index = group_indexes[group_index]
+            choices[choice_index][1].append(self.make_choice(item))
+        return choices
+    
     def make_choice(self, obj):
         return (obj.pk, "   " + self.label_from_instance(obj))
@digi604
Owner

please open a pull request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.