Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7738: support initial values via `GET` for `SelectMutliple` in…

… the admin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8699 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 21928f2ba01c5411c57720eec82f7ad8658ed733 1 parent 89ccbab
Jacob Kaplan-Moss authored August 29, 2008
12  django/contrib/admin/options.py
@@ -503,7 +503,17 @@ def add_view(self, request, form_url='', extra_context=None):
503 503
                 self.log_addition(request, new_object)
504 504
                 return self.response_add(request, new_object)
505 505
         else:
506  
-            form = ModelForm(initial=dict(request.GET.items()))
  506
+            # Prepare the dict of initial data from the request.
  507
+            # We have to special-case M2Ms as a list of comma-separated PKs.
  508
+            initial = dict(request.GET.items())
  509
+            for k in initial:
  510
+                try:
  511
+                    f = opts.get_field(k)
  512
+                except FieldDoesNotExist:
  513
+                    pass
  514
+                if isinstance(f, models.ManyToManyField):
  515
+                    initial[k] = initial[k].split(",")
  516
+            form = ModelForm(initial=initial)
507 517
             for FormSet in self.get_formsets(request):
508 518
                 formset = FormSet(instance=self.model())
509 519
                 formsets.append(formset)
8  tests/regressiontests/admin_views/tests.py
@@ -36,6 +36,14 @@ def testBasicAddGet(self):
36 36
         response = self.client.get('/test_admin/admin/admin_views/section/add/')
37 37
         self.failUnlessEqual(response.status_code, 200)
38 38
     
  39
+    def testAddWithGETArgs(self):
  40
+        response = self.client.get('/test_admin/admin/admin_views/section/add/', {'name': 'My Section'})
  41
+        self.failUnlessEqual(response.status_code, 200)
  42
+        self.failUnless(
  43
+            'value="My Section"' in response.content, 
  44
+            "Couldn't find an input with the right value in the response."
  45
+        )
  46
+    
39 47
     def testBasicEditGet(self):
40 48
         """
41 49
         A smoke test to ensureGET on the change_view works.

0 notes on commit 21928f2

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