Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14880 - raw_id_fields in admin does not work when limi…

…t_choices_to dictionary has value=False

Thanks to smallming for the report.

Backport of [15348] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15351 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b3ad41bfd51562e5d18350b18650ad4638ed0869 1 parent 276ecc7
Luke Plant authored January 28, 2011
3  django/contrib/admin/widgets.py
@@ -111,6 +111,9 @@ def url_params_from_lookup_dict(lookups):
111 111
         for k, v in lookups.items():
112 112
             if isinstance(v, list):
113 113
                 v = u','.join([str(x) for x in v])
  114
+            elif isinstance(v, bool):
  115
+                # See django.db.fields.BooleanField.get_prep_lookup
  116
+                v = ('0', '1')[v]
114 117
             else:
115 118
                 v = unicode(v)
116 119
             items.append((k, v))
16  tests/regressiontests/admin_views/models.py
@@ -160,17 +160,27 @@ def __unicode__(self):
160 160
 class Inquisition(models.Model):
161 161
     expected = models.BooleanField()
162 162
     leader = models.ForeignKey(Actor)
  163
+    country = models.CharField(max_length=20)
  164
+
163 165
     def __unicode__(self):
164  
-        return self.expected
  166
+        return u"by %s from %s" % (self.leader, self.country)
  167
+
  168
+class InquisitionAdmin(admin.ModelAdmin):
  169
+    list_display = ('leader', 'country', 'expected')
165 170
 
166 171
 class Sketch(models.Model):
167 172
     title = models.CharField(max_length=100)
168 173
     inquisition = models.ForeignKey(Inquisition, limit_choices_to={'leader__name': 'Palin',
169 174
                                                                    'leader__age': 27,
  175
+                                                                   'expected': False,
170 176
                                                                    })
  177
+
171 178
     def __unicode__(self):
172 179
         return self.title
173 180
 
  181
+class SketchAdmin(admin.ModelAdmin):
  182
+    raw_id_fields = ('inquisition',)
  183
+
174 184
 class Fabric(models.Model):
175 185
     NG_CHOICES = (
176 186
         ('Textured', (
@@ -626,8 +636,8 @@ class WorkHourAdmin(admin.ModelAdmin):
626 636
 admin.site.register(Color)
627 637
 admin.site.register(Thing, ThingAdmin)
628 638
 admin.site.register(Actor)
629  
-admin.site.register(Inquisition)
630  
-admin.site.register(Sketch)
  639
+admin.site.register(Inquisition, InquisitionAdmin)
  640
+admin.site.register(Sketch, SketchAdmin)
631 641
 admin.site.register(Person, PersonAdmin)
632 642
 admin.site.register(Persona, PersonaAdmin)
633 643
 admin.site.register(Subscriber, SubscriberAdmin)
36  tests/regressiontests/admin_views/tests.py
@@ -2,6 +2,7 @@
2 2
 
3 3
 import re
4 4
 import datetime
  5
+import urlparse
5 6
 
6 7
 from django.conf import settings
7 8
 from django.core.exceptions import SuspiciousOperation
@@ -28,7 +29,7 @@
28 29
     FooAccount, Gallery, ModelWithStringPrimaryKey, \
29 30
     Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
30 31
     Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
31  
-    Category, Post, Plot, FunkyTag, WorkHour, Employee
  32
+    Category, Post, Plot, FunkyTag, WorkHour, Employee, Inquisition, Actor
32 33
 
33 34
 
34 35
 class AdminViewBasicTest(TestCase):
@@ -2181,6 +2182,39 @@ def test_readonly_manytomany(self):
2181 2182
         response = self.client.get('/test_admin/admin/admin_views/pizza/add/')
2182 2183
         self.assertEqual(response.status_code, 200)
2183 2184
 
  2185
+
  2186
+class RawIdFieldsTest(TestCase):
  2187
+    fixtures = ['admin-views-users.xml']
  2188
+
  2189
+    def setUp(self):
  2190
+        self.client.login(username='super', password='secret')
  2191
+
  2192
+    def tearDown(self):
  2193
+        self.client.logout()
  2194
+
  2195
+    def test_limit_choices_to(self):
  2196
+        """Regression test for 14880"""
  2197
+        # This includes tests integers, strings and booleans in the lookup query string
  2198
+        actor = Actor.objects.create(name="Palin", age=27)
  2199
+        inquisition1 = Inquisition.objects.create(expected=True,
  2200
+                                                  leader=actor,
  2201
+                                                  country="England")
  2202
+        inquisition2 = Inquisition.objects.create(expected=False,
  2203
+                                                  leader=actor,
  2204
+                                                  country="Spain")
  2205
+        response = self.client.get('/test_admin/admin/admin_views/sketch/add/')
  2206
+        # Find the link
  2207
+        m = re.search(r'<a href="([^"]*)"[^>]* id="lookup_id_inquisition"', response.content)
  2208
+        self.assertTrue(m) # Got a match
  2209
+        popup_url = m.groups()[0].replace("&amp;", "&")
  2210
+
  2211
+        # Handle relative links
  2212
+        popup_url = urlparse.urljoin(response.request['PATH_INFO'], popup_url)
  2213
+        # Get the popup
  2214
+        response2 = self.client.get(popup_url)
  2215
+        self.assertContains(response2, "Spain")
  2216
+        self.assertNotContains(response2, "England")
  2217
+
2184 2218
 class UserAdminTest(TestCase):
2185 2219
     """
2186 2220
     Tests user CRUD functionality.

0 notes on commit b3ad41b

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