Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14880 - raw_id_fields in admin does not work when limit_choice…

…s_to dictionary has value=False

Thanks to smallming for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15348 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 655d5afea9d1f3d5aa55efc9fe77a14b353e35a6 1 parent c24bdf0
Luke Plant authored January 28, 2011
3  django/contrib/admin/widgets.py
@@ -102,6 +102,9 @@ def url_params_from_lookup_dict(lookups):
102 102
         for k, v in lookups.items():
103 103
             if isinstance(v, list):
104 104
                 v = u','.join([str(x) for x in v])
  105
+            elif isinstance(v, bool):
  106
+                # See django.db.fields.BooleanField.get_prep_lookup
  107
+                v = ('0', '1')[v]
105 108
             else:
106 109
                 v = unicode(v)
107 110
             items.append((k, v))
16  tests/regressiontests/admin_views/models.py
@@ -187,17 +187,27 @@ def __unicode__(self):
187 187
 class Inquisition(models.Model):
188 188
     expected = models.BooleanField()
189 189
     leader = models.ForeignKey(Actor)
  190
+    country = models.CharField(max_length=20)
  191
+
190 192
     def __unicode__(self):
191  
-        return self.expected
  193
+        return u"by %s from %s" % (self.leader, self.country)
  194
+
  195
+class InquisitionAdmin(admin.ModelAdmin):
  196
+    list_display = ('leader', 'country', 'expected')
192 197
 
193 198
 class Sketch(models.Model):
194 199
     title = models.CharField(max_length=100)
195 200
     inquisition = models.ForeignKey(Inquisition, limit_choices_to={'leader__name': 'Palin',
196 201
                                                                    'leader__age': 27,
  202
+                                                                   'expected': False,
197 203
                                                                    })
  204
+
198 205
     def __unicode__(self):
199 206
         return self.title
200 207
 
  208
+class SketchAdmin(admin.ModelAdmin):
  209
+    raw_id_fields = ('inquisition',)
  210
+
201 211
 class Fabric(models.Model):
202 212
     NG_CHOICES = (
203 213
         ('Textured', (
@@ -663,8 +673,8 @@ def __unicode__(self):
663 673
 admin.site.register(Color)
664 674
 admin.site.register(Thing, ThingAdmin)
665 675
 admin.site.register(Actor)
666  
-admin.site.register(Inquisition)
667  
-admin.site.register(Sketch)
  676
+admin.site.register(Inquisition, InquisitionAdmin)
  677
+admin.site.register(Sketch, SketchAdmin)
668 678
 admin.site.register(Person, PersonAdmin)
669 679
 admin.site.register(Persona, PersonaAdmin)
670 680
 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 import mail
@@ -34,7 +35,7 @@
34 35
     Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
35 36
     Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
36 37
     Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
37  
-    Question, Answer)
  38
+    Question, Answer, Inquisition, Actor)
38 39
 
39 40
 
40 41
 class AdminViewBasicTest(TestCase):
@@ -2350,6 +2351,39 @@ def test_readonly_manytomany(self):
2350 2351
         response = self.client.get('/test_admin/admin/admin_views/pizza/add/')
2351 2352
         self.assertEqual(response.status_code, 200)
2352 2353
 
  2354
+
  2355
+class RawIdFieldsTest(TestCase):
  2356
+    fixtures = ['admin-views-users.xml']
  2357
+
  2358
+    def setUp(self):
  2359
+        self.client.login(username='super', password='secret')
  2360
+
  2361
+    def tearDown(self):
  2362
+        self.client.logout()
  2363
+
  2364
+    def test_limit_choices_to(self):
  2365
+        """Regression test for 14880"""
  2366
+        # This includes tests integers, strings and booleans in the lookup query string
  2367
+        actor = Actor.objects.create(name="Palin", age=27)
  2368
+        inquisition1 = Inquisition.objects.create(expected=True,
  2369
+                                                  leader=actor,
  2370
+                                                  country="England")
  2371
+        inquisition2 = Inquisition.objects.create(expected=False,
  2372
+                                                  leader=actor,
  2373
+                                                  country="Spain")
  2374
+        response = self.client.get('/test_admin/admin/admin_views/sketch/add/')
  2375
+        # Find the link
  2376
+        m = re.search(r'<a href="([^"]*)"[^>]* id="lookup_id_inquisition"', response.content)
  2377
+        self.assertTrue(m) # Got a match
  2378
+        popup_url = m.groups()[0].replace("&amp;", "&")
  2379
+
  2380
+        # Handle relative links
  2381
+        popup_url = urlparse.urljoin(response.request['PATH_INFO'], popup_url)
  2382
+        # Get the popup
  2383
+        response2 = self.client.get(popup_url)
  2384
+        self.assertContains(response2, "Spain")
  2385
+        self.assertNotContains(response2, "England")
  2386
+
2353 2387
 class UserAdminTest(TestCase):
2354 2388
     """
2355 2389
     Tests user CRUD functionality.

0 notes on commit 655d5af

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