Ticket #9739: 9739-r16345.diff

File 9739-r16345.diff, 3.4 KB (added by dArignac, 4 years ago)
  • docs/ref/contrib/admin/index.txt

     
    18171817URL names for the purposes of :ref:`reversing them<admin-reverse-urls>`. This
    18181818is only necessary if you are using more than one ``AdminSite``.
    18191819
     1820Pre-populating fields in admin sites
     1821--------------------------------
     1822It's easy to offer prefilled values for the fields of an admin site.
     1823Simply append the value to the URL as GET parameters with the name of the
     1824field as key, e.g.:
     1825
     1826    /admin/model/add/?forename=John&surname=Doe
     1827   
     1828Related fields can also be prefilled:
     1829
     1830    /admin/modelname/add/?surname=Doe&relatedfieldvalue=2
     1831
    18201832Adding views to admin sites
    18211833---------------------------
    18221834
  • django/contrib/admin/options.py

     
    911911                    continue
    912912                if isinstance(f, models.ManyToManyField):
    913913                    initial[k] = initial[k].split(",")
     914                if isinstance(f, models.DateTimeField):
     915                    initial[k] = initial[k].split(",")
    914916            form = ModelForm(initial=initial)
    915917            prefixes = {}
    916918            for FormSet, inline in zip(self.get_formsets(request),
  • tests/regressiontests/admin_views/tests.py

     
    535535            self.client.get("/test_admin/admin/admin_views/inquisition/?leader__name=Palin&leader__age=27")
    536536        except SuspiciousOperation:
    537537            self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")
     538   
     539    def test_field_prepopulation(self):
     540        """
     541        Regression test for #9739
     542        Tests the prepopulation of fields within the admin site.
     543        """
     544        # prepare some sections
     545        section_0 = Section.objects.create(name='Section 1')
     546        section_1 = Section.objects.create(name='Section 2')
     547        section_2 = Section.objects.create(name='Section 3')
     548        # call the addition page for articles
     549        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')
     550        self.assertEqual(response.status_code, 200)
     551        # test if the prepopulated values are within the response and in the correct fields
     552        self.assertRegexpMatches(response.content, r'<input (?=.*name="title")(?=.*value="Testtitle").* />')
     553        self.assertRegexpMatches(response.content, r'<textarea (?=.*name="content").*>Lorem Ipsum</textarea>')
     554        self.assertRegexpMatches(response.content, r'<input (?=.*name="date_0")(?=.*value="2011-06-01").* />')
     555        self.assertRegexpMatches(response.content, r'<input (?=.*name="date_1")(?=.*value="18:21:53").* />')
     556        self.assertRegexpMatches(response.content, r'<option (?=.*value="2")(?=.*selected="selected").*>Section object</option>')
     557       
    538558
    539559class AdminJavaScriptTest(AdminViewBasicTest):
    540560    def testSingleWidgetFirsFieldFocus(self):
Back to Top