Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@jacobian jacobian authored
View
12 django/contrib/admin/options.py
@@ -503,7 +503,17 @@ def add_view(self, request, form_url='', extra_context=None):
self.log_addition(request, new_object)
return self.response_add(request, new_object)
else:
- form = ModelForm(initial=dict(request.GET.items()))
+ # Prepare the dict of initial data from the request.
+ # We have to special-case M2Ms as a list of comma-separated PKs.
+ initial = dict(request.GET.items())
+ for k in initial:
+ try:
+ f = opts.get_field(k)
+ except FieldDoesNotExist:
+ pass
+ if isinstance(f, models.ManyToManyField):
+ initial[k] = initial[k].split(",")
+ form = ModelForm(initial=initial)
for FormSet in self.get_formsets(request):
formset = FormSet(instance=self.model())
formsets.append(formset)
View
8 tests/regressiontests/admin_views/tests.py
@@ -36,6 +36,14 @@ def testBasicAddGet(self):
response = self.client.get('/test_admin/admin/admin_views/section/add/')
self.failUnlessEqual(response.status_code, 200)
+ def testAddWithGETArgs(self):
+ response = self.client.get('/test_admin/admin/admin_views/section/add/', {'name': 'My Section'})
+ self.failUnlessEqual(response.status_code, 200)
+ self.failUnless(
+ 'value="My Section"' in response.content,
+ "Couldn't find an input with the right value in the response."
+ )
+
def testBasicEditGet(self):
"""
A smoke test to ensureGET on the change_view works.
Please sign in to comment.
Something went wrong with that request. Please try again.