Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12024: Changed admin code to avoid raising an exception when a…

… field listed

in raw_id_fields has limit_choices_to specified as a Q object. 

Tweaked a test to trigger the condition and verify the fix. 

Finally, documented that limit_choices_to specified as a Q object has no effect 
on the choices available for fields listed in raw_id_fields, and removed another 
incorrect note that claimed limit_choices_to had no effect on inlines in the admin.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12728 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f2bc4dd0a9e4676f77d4a1c0ddb5e836750f966c 1 parent fa3b4a4
Karen Tracey authored March 08, 2010
2  django/contrib/admin/widgets.py
@@ -133,7 +133,7 @@ def render(self, name, value, attrs=None):
133 133
 
134 134
     def base_url_parameters(self):
135 135
         params = {}
136  
-        if self.rel.limit_choices_to:
  136
+        if self.rel.limit_choices_to and hasattr(self.rel.limit_choices_to, 'items'):
137 137
             items = []
138 138
             for k, v in self.rel.limit_choices_to.items():
139 139
                 if isinstance(v, list):
8  docs/ref/models/fields.txt
@@ -872,10 +872,10 @@ define the details of how the relation works.
872 872
     current date/time to be chosen.
873 873
 
874 874
     Instead of a dictionary this can also be a :class:`~django.db.models.Q`
875  
-    object for more :ref:`complex queries <complex-lookups-with-q>`.
876  
-
877  
-    ``limit_choices_to`` has no effect on the inline FormSets that are created
878  
-    to display related objects in the admin.
  875
+    object for more :ref:`complex queries <complex-lookups-with-q>`. However, 
  876
+    if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it 
  877
+    will only have an effect on the choices available in the admin when the 
  878
+    field is not listed in ``raw_id_fields`` in the ``ModelAdmin`` for the model.
879 879
 
880 880
 .. attribute:: ForeignKey.related_name
881 881
 
2  tests/regressiontests/admin_widgets/models.py
@@ -49,7 +49,7 @@ def __unicode__(self):
49 49
       return self.name
50 50
 
51 51
 class Event(models.Model):
52  
-    band = models.ForeignKey(Band)
  52
+    band = models.ForeignKey(Band, limit_choices_to=models.Q(pk__gt=0))
53 53
     start_date = models.DateField(blank=True, null=True)
54 54
     start_time = models.TimeField(blank=True, null=True)
55 55
     description = models.TextField(blank=True)

0 notes on commit f2bc4dd

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