Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7475: fixed a possible race condition in ModelChoiceIterator. …

…Thanks, esaj.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7710 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e637f4723225d630e6066a04558a3549c83f8a5a 1 parent 5d09d87
Jacob Kaplan-Moss authored June 19, 2008

Showing 1 changed file with 12 additions and 5 deletions. Show diff stats Hide diff stats

  1. 17  django/newforms/models.py
17  django/newforms/models.py
@@ -285,11 +285,17 @@ def __init__(self, field):
285 285
     def __iter__(self):
286 286
         if self.field.empty_label is not None:
287 287
             yield (u"", self.field.empty_label)
288  
-        for obj in self.queryset:
289  
-            yield (obj.pk, self.field.label_from_instance(obj))
290  
-        # Clear the QuerySet cache if required.
291  
-        if not self.field.cache_choices:
292  
-            self.queryset._result_cache = None
  288
+        if self.field.cache_choices:
  289
+            if self.field.choice_cache is None:
  290
+                self.field.choice_cache = [
  291
+                    (obj.pk, self.field.label_from_instance(obj))
  292
+                    for obj in self.queryset.all()
  293
+                ]
  294
+            for choice in self.field.choice_cache:
  295
+                yield choice
  296
+        else:
  297
+            for obj in self.queryset.all():
  298
+                yield (obj.pk, self.field.label_from_instance(obj))
293 299
 
294 300
 class ModelChoiceField(ChoiceField):
295 301
     """A ChoiceField whose choices are a model QuerySet."""
@@ -311,6 +317,7 @@ def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
311 317
         Field.__init__(self, required, widget, label, initial, help_text,
312 318
                        *args, **kwargs)
313 319
         self.queryset = queryset
  320
+        self.choice_cache = None
314 321
 
315 322
     def _get_queryset(self):
316 323
         return self._queryset

0 notes on commit e637f47

Please sign in to comment.
Something went wrong with that request. Please try again.