MCPcopy
hub / github.com/django/django / test_subset_fields

Method test_subset_fields

tests/model_forms/tests.py:1806–1866  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1804 )
1805
1806 def test_subset_fields(self):
1807 # You can restrict a form to a subset of the complete list of fields
1808 # by providing a 'fields' argument. If you try to save a
1809 # model created with such a form, you need to ensure that the fields
1810 # that are _not_ on the form have default values, or are allowed to
1811 # have a value of None. If a field isn't specified on a form, the
1812 # object created from the form can't provide a value for that field!
1813 class PartialArticleForm(forms.ModelForm):
1814 class Meta:
1815 model = Article
1816 fields = ("headline", "pub_date")
1817
1818 f = PartialArticleForm(auto_id=False)
1819 self.assertHTMLEqual(
1820 str(f),
1821 '<div>Headline:<input type="text" name="headline" maxlength="50" required>'
1822 '</div><div>Pub date:<input type="text" name="pub_date" required></div>',
1823 )
1824
1825 class PartialArticleFormWithSlug(forms.ModelForm):
1826 class Meta:
1827 model = Article
1828 fields = ("headline", "slug", "pub_date")
1829
1830 w_royko = Writer.objects.create(name="Mike Royko")
1831 art = Article.objects.create(
1832 article="Hello.",
1833 headline="New headline",
1834 slug="new-headline",
1835 pub_date=datetime.date(1988, 1, 4),
1836 writer=w_royko,
1837 )
1838 f = PartialArticleFormWithSlug(
1839 {
1840 "headline": "New headline",
1841 "slug": "new-headline",
1842 "pub_date": "1988-01-04",
1843 },
1844 auto_id=False,
1845 instance=art,
1846 )
1847 self.assertHTMLEqual(
1848 f.as_ul(),
1849 """
1850 <li>Headline:
1851 <input type="text" name="headline" value="New headline" maxlength="50"
1852 required>
1853 </li>
1854 <li>Slug:
1855 <input type="text" name="slug" value="new-headline" maxlength="50"
1856 required>
1857 </li>
1858 <li>Pub date:
1859 <input type="text" name="pub_date" value="1988-01-04" required></li>
1860 """,
1861 )
1862 self.assertTrue(f.is_valid())
1863 new_art = f.save()

Callers

nothing calls this directly

Calls 8

PartialArticleFormClass · 0.85
assertHTMLEqualMethod · 0.80
createMethod · 0.45
as_ulMethod · 0.45
is_validMethod · 0.45
saveMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected