Code

Ticket #9739: 9739-r16345.diff

File 9739-r16345.diff, 3.4 KB (added by dArignac, 3 years ago)
Line 
1Index: docs/ref/contrib/admin/index.txt
2===================================================================
3--- docs/ref/contrib/admin/index.txt    (Revision 16345)
4+++ docs/ref/contrib/admin/index.txt    (Arbeitskopie)
5@@ -1817,6 +1817,18 @@
6 URL names for the purposes of :ref:`reversing them<admin-reverse-urls>`. This
7 is only necessary if you are using more than one ``AdminSite``.
8 
9+Pre-populating fields in admin sites
10+--------------------------------
11+It's easy to offer prefilled values for the fields of an admin site.
12+Simply append the value to the URL as GET parameters with the name of the
13+field as key, e.g.:
14+
15+    /admin/model/add/?forename=John&surname=Doe
16+   
17+Related fields can also be prefilled:
18+
19+    /admin/modelname/add/?surname=Doe&relatedfieldvalue=2
20+
21 Adding views to admin sites
22 ---------------------------
23 
24Index: django/contrib/admin/options.py
25===================================================================
26--- django/contrib/admin/options.py     (Revision 16345)
27+++ django/contrib/admin/options.py     (Arbeitskopie)
28@@ -911,6 +911,8 @@
29                     continue
30                 if isinstance(f, models.ManyToManyField):
31                     initial[k] = initial[k].split(",")
32+                if isinstance(f, models.DateTimeField):
33+                    initial[k] = initial[k].split(",")
34             form = ModelForm(initial=initial)
35             prefixes = {}
36             for FormSet, inline in zip(self.get_formsets(request),
37Index: tests/regressiontests/admin_views/tests.py
38===================================================================
39--- tests/regressiontests/admin_views/tests.py  (Revision 16345)
40+++ tests/regressiontests/admin_views/tests.py  (Arbeitskopie)
41@@ -535,6 +535,26 @@
42             self.client.get("/test_admin/admin/admin_views/inquisition/?leader__name=Palin&leader__age=27")
43         except SuspiciousOperation:
44             self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")
45+   
46+    def test_field_prepopulation(self):
47+        """
48+        Regression test for #9739
49+        Tests the prepopulation of fields within the admin site.
50+        """
51+        # prepare some sections
52+        section_0 = Section.objects.create(name='Section 1')
53+        section_1 = Section.objects.create(name='Section 2')
54+        section_2 = Section.objects.create(name='Section 3')
55+        # call the addition page for articles
56+        response = self.client.get('/test_admin/' + self.urlbit + '/admin_views/article/add/?title=Testtitle&content=Lorem%20Ipsum&date=2011-06-01,18:21:53&section=2')
57+        self.assertEqual(response.status_code, 200)
58+        # test if the prepopulated values are within the response and in the correct fields
59+        self.assertRegexpMatches(response.content, r'<input (?=.*name="title")(?=.*value="Testtitle").* />')
60+        self.assertRegexpMatches(response.content, r'<textarea (?=.*name="content").*>Lorem Ipsum</textarea>')
61+        self.assertRegexpMatches(response.content, r'<input (?=.*name="date_0")(?=.*value="2011-06-01").* />')
62+        self.assertRegexpMatches(response.content, r'<input (?=.*name="date_1")(?=.*value="18:21:53").* />')
63+        self.assertRegexpMatches(response.content, r'<option (?=.*value="2")(?=.*selected="selected").*>Section object</option>')
64+       
65 
66 class AdminJavaScriptTest(AdminViewBasicTest):
67     def testSingleWidgetFirsFieldFocus(self):