PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/modeltests/model_forms/models.py

https://code.google.com/p/mango-py/
Python | 1613 lines | 1566 code | 30 blank | 17 comment | 5 complexity | 4b4e882b4960f284479369ffd12237dc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. XX. Generating HTML forms from models
  3. This is mostly just a reworking of the ``form_for_model``/``form_for_instance``
  4. tests to use ``ModelForm``. As such, the text may not make sense in all cases,
  5. and the examples are probably a poor fit for the ``ModelForm`` syntax. In other
  6. words, most of these tests should be rewritten.
  7. """
  8. import os
  9. import tempfile
  10. from django.db import models
  11. from django.core.files.storage import FileSystemStorage
  12. temp_storage_dir = tempfile.mkdtemp()
  13. temp_storage = FileSystemStorage(temp_storage_dir)
  14. ARTICLE_STATUS = (
  15. (1, 'Draft'),
  16. (2, 'Pending'),
  17. (3, 'Live'),
  18. )
  19. ARTICLE_STATUS_CHAR = (
  20. ('d', 'Draft'),
  21. ('p', 'Pending'),
  22. ('l', 'Live'),
  23. )
  24. class Category(models.Model):
  25. name = models.CharField(max_length=20)
  26. slug = models.SlugField(max_length=20)
  27. url = models.CharField('The URL', max_length=40)
  28. def __unicode__(self):
  29. return self.name
  30. class Writer(models.Model):
  31. name = models.CharField(max_length=50, help_text='Use both first and last names.')
  32. class Meta:
  33. ordering = ('name',)
  34. def __unicode__(self):
  35. return self.name
  36. class Article(models.Model):
  37. headline = models.CharField(max_length=50)
  38. slug = models.SlugField()
  39. pub_date = models.DateField()
  40. created = models.DateField(editable=False)
  41. writer = models.ForeignKey(Writer)
  42. article = models.TextField()
  43. categories = models.ManyToManyField(Category, blank=True)
  44. status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
  45. def save(self):
  46. import datetime
  47. if not self.id:
  48. self.created = datetime.date.today()
  49. return super(Article, self).save()
  50. def __unicode__(self):
  51. return self.headline
  52. class ImprovedArticle(models.Model):
  53. article = models.OneToOneField(Article)
  54. class ImprovedArticleWithParentLink(models.Model):
  55. article = models.OneToOneField(Article, parent_link=True)
  56. class BetterWriter(Writer):
  57. score = models.IntegerField()
  58. class WriterProfile(models.Model):
  59. writer = models.OneToOneField(Writer, primary_key=True)
  60. age = models.PositiveIntegerField()
  61. def __unicode__(self):
  62. return "%s is %s" % (self.writer, self.age)
  63. from django.contrib.localflavor.us.models import PhoneNumberField
  64. class PhoneNumber(models.Model):
  65. phone = PhoneNumberField()
  66. description = models.CharField(max_length=20)
  67. def __unicode__(self):
  68. return self.phone
  69. class TextFile(models.Model):
  70. description = models.CharField(max_length=20)
  71. file = models.FileField(storage=temp_storage, upload_to='tests', max_length=15)
  72. def __unicode__(self):
  73. return self.description
  74. try:
  75. # If PIL is available, try testing ImageFields. Checking for the existence
  76. # of Image is enough for CPython, but for PyPy, you need to check for the
  77. # underlying modules If PIL is not available, ImageField tests are omitted.
  78. # Try to import PIL in either of the two ways it can end up installed.
  79. try:
  80. from PIL import Image, _imaging
  81. except ImportError:
  82. import Image, _imaging
  83. test_images = True
  84. class ImageFile(models.Model):
  85. def custom_upload_path(self, filename):
  86. path = self.path or 'tests'
  87. return '%s/%s' % (path, filename)
  88. description = models.CharField(max_length=20)
  89. # Deliberately put the image field *after* the width/height fields to
  90. # trigger the bug in #10404 with width/height not getting assigned.
  91. width = models.IntegerField(editable=False)
  92. height = models.IntegerField(editable=False)
  93. image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
  94. width_field='width', height_field='height')
  95. path = models.CharField(max_length=16, blank=True, default='')
  96. def __unicode__(self):
  97. return self.description
  98. class OptionalImageFile(models.Model):
  99. def custom_upload_path(self, filename):
  100. path = self.path or 'tests'
  101. return '%s/%s' % (path, filename)
  102. description = models.CharField(max_length=20)
  103. image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
  104. width_field='width', height_field='height',
  105. blank=True, null=True)
  106. width = models.IntegerField(editable=False, null=True)
  107. height = models.IntegerField(editable=False, null=True)
  108. path = models.CharField(max_length=16, blank=True, default='')
  109. def __unicode__(self):
  110. return self.description
  111. except ImportError:
  112. test_images = False
  113. class CommaSeparatedInteger(models.Model):
  114. field = models.CommaSeparatedIntegerField(max_length=20)
  115. def __unicode__(self):
  116. return self.field
  117. class Product(models.Model):
  118. slug = models.SlugField(unique=True)
  119. def __unicode__(self):
  120. return self.slug
  121. class Price(models.Model):
  122. price = models.DecimalField(max_digits=10, decimal_places=2)
  123. quantity = models.PositiveIntegerField()
  124. def __unicode__(self):
  125. return u"%s for %s" % (self.quantity, self.price)
  126. class Meta:
  127. unique_together = (('price', 'quantity'),)
  128. class ArticleStatus(models.Model):
  129. status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
  130. class Inventory(models.Model):
  131. barcode = models.PositiveIntegerField(unique=True)
  132. parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
  133. name = models.CharField(blank=False, max_length=20)
  134. class Meta:
  135. ordering = ('name',)
  136. def __unicode__(self):
  137. return self.name
  138. class Book(models.Model):
  139. title = models.CharField(max_length=40)
  140. author = models.ForeignKey(Writer, blank=True, null=True)
  141. special_id = models.IntegerField(blank=True, null=True, unique=True)
  142. class Meta:
  143. unique_together = ('title', 'author')
  144. class BookXtra(models.Model):
  145. isbn = models.CharField(max_length=16, unique=True)
  146. suffix1 = models.IntegerField(blank=True, default=0)
  147. suffix2 = models.IntegerField(blank=True, default=0)
  148. class Meta:
  149. unique_together = (('suffix1', 'suffix2'))
  150. abstract = True
  151. class DerivedBook(Book, BookXtra):
  152. pass
  153. class ExplicitPK(models.Model):
  154. key = models.CharField(max_length=20, primary_key=True)
  155. desc = models.CharField(max_length=20, blank=True, unique=True)
  156. class Meta:
  157. unique_together = ('key', 'desc')
  158. def __unicode__(self):
  159. return self.key
  160. class Post(models.Model):
  161. title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
  162. slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
  163. subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
  164. posted = models.DateField()
  165. def __unicode__(self):
  166. return self.name
  167. class DerivedPost(Post):
  168. pass
  169. class BigInt(models.Model):
  170. biggie = models.BigIntegerField()
  171. def __unicode__(self):
  172. return unicode(self.biggie)
  173. class MarkupField(models.CharField):
  174. def __init__(self, *args, **kwargs):
  175. kwargs["max_length"] = 20
  176. super(MarkupField, self).__init__(*args, **kwargs)
  177. def formfield(self, **kwargs):
  178. # don't allow this field to be used in form (real use-case might be
  179. # that you know the markup will always be X, but it is among an app
  180. # that allows the user to say it could be something else)
  181. # regressed at r10062
  182. return None
  183. class CustomFieldForExclusionModel(models.Model):
  184. name = models.CharField(max_length=10)
  185. markup = MarkupField()
  186. class FlexibleDatePost(models.Model):
  187. title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
  188. slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
  189. subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
  190. posted = models.DateField(blank=True, null=True)
  191. __test__ = {'API_TESTS': """
  192. >>> from django import forms
  193. >>> from django.forms.models import ModelForm, model_to_dict
  194. >>> from django.core.files.uploadedfile import SimpleUploadedFile
  195. The bare bones, absolutely nothing custom, basic case.
  196. >>> class CategoryForm(ModelForm):
  197. ... class Meta:
  198. ... model = Category
  199. >>> CategoryForm.base_fields.keys()
  200. ['name', 'slug', 'url']
  201. Extra fields.
  202. >>> class CategoryForm(ModelForm):
  203. ... some_extra_field = forms.BooleanField()
  204. ...
  205. ... class Meta:
  206. ... model = Category
  207. >>> CategoryForm.base_fields.keys()
  208. ['name', 'slug', 'url', 'some_extra_field']
  209. Extra field that has a name collision with a related object accessor.
  210. >>> class WriterForm(ModelForm):
  211. ... book = forms.CharField(required=False)
  212. ...
  213. ... class Meta:
  214. ... model = Writer
  215. >>> wf = WriterForm({'name': 'Richard Lockridge'})
  216. >>> wf.is_valid()
  217. True
  218. Replacing a field.
  219. >>> class CategoryForm(ModelForm):
  220. ... url = forms.BooleanField()
  221. ...
  222. ... class Meta:
  223. ... model = Category
  224. >>> CategoryForm.base_fields['url'].__class__
  225. <class 'django.forms.fields.BooleanField'>
  226. Using 'fields'.
  227. >>> class CategoryForm(ModelForm):
  228. ...
  229. ... class Meta:
  230. ... model = Category
  231. ... fields = ['url']
  232. >>> CategoryForm.base_fields.keys()
  233. ['url']
  234. Using 'exclude'
  235. >>> class CategoryForm(ModelForm):
  236. ...
  237. ... class Meta:
  238. ... model = Category
  239. ... exclude = ['url']
  240. >>> CategoryForm.base_fields.keys()
  241. ['name', 'slug']
  242. Using 'fields' *and* 'exclude'. Not sure why you'd want to do this, but uh,
  243. "be liberal in what you accept" and all.
  244. >>> class CategoryForm(ModelForm):
  245. ...
  246. ... class Meta:
  247. ... model = Category
  248. ... fields = ['name', 'url']
  249. ... exclude = ['url']
  250. >>> CategoryForm.base_fields.keys()
  251. ['name']
  252. Using 'widgets'
  253. >>> class CategoryForm(ModelForm):
  254. ...
  255. ... class Meta:
  256. ... model = Category
  257. ... fields = ['name', 'url', 'slug']
  258. ... widgets = {
  259. ... 'name': forms.Textarea,
  260. ... 'url': forms.TextInput(attrs={'class': 'url'})
  261. ... }
  262. >>> str(CategoryForm()['name'])
  263. '<textarea id="id_name" rows="10" cols="40" name="name"></textarea>'
  264. >>> str(CategoryForm()['url'])
  265. '<input id="id_url" type="text" class="url" name="url" maxlength="40" />'
  266. >>> str(CategoryForm()['slug'])
  267. '<input id="id_slug" type="text" name="slug" maxlength="20" />'
  268. Don't allow more than one 'model' definition in the inheritance hierarchy.
  269. Technically, it would generate a valid form, but the fact that the resulting
  270. save method won't deal with multiple objects is likely to trip up people not
  271. familiar with the mechanics.
  272. >>> class CategoryForm(ModelForm):
  273. ... class Meta:
  274. ... model = Category
  275. >>> class OddForm(CategoryForm):
  276. ... class Meta:
  277. ... model = Article
  278. OddForm is now an Article-related thing, because BadForm.Meta overrides
  279. CategoryForm.Meta.
  280. >>> OddForm.base_fields.keys()
  281. ['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
  282. >>> class ArticleForm(ModelForm):
  283. ... class Meta:
  284. ... model = Article
  285. First class with a Meta class wins.
  286. >>> class BadForm(ArticleForm, CategoryForm):
  287. ... pass
  288. >>> OddForm.base_fields.keys()
  289. ['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
  290. Subclassing without specifying a Meta on the class will use the parent's Meta
  291. (or the first parent in the MRO if there are multiple parent classes).
  292. >>> class CategoryForm(ModelForm):
  293. ... class Meta:
  294. ... model = Category
  295. >>> class SubCategoryForm(CategoryForm):
  296. ... pass
  297. >>> SubCategoryForm.base_fields.keys()
  298. ['name', 'slug', 'url']
  299. We can also subclass the Meta inner class to change the fields list.
  300. >>> class CategoryForm(ModelForm):
  301. ... checkbox = forms.BooleanField()
  302. ...
  303. ... class Meta:
  304. ... model = Category
  305. >>> class SubCategoryForm(CategoryForm):
  306. ... class Meta(CategoryForm.Meta):
  307. ... exclude = ['url']
  308. >>> print SubCategoryForm()
  309. <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
  310. <tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr>
  311. <tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr>
  312. # test using fields to provide ordering to the fields
  313. >>> class CategoryForm(ModelForm):
  314. ... class Meta:
  315. ... model = Category
  316. ... fields = ['url', 'name']
  317. >>> CategoryForm.base_fields.keys()
  318. ['url', 'name']
  319. >>> print CategoryForm()
  320. <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
  321. <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
  322. >>> class CategoryForm(ModelForm):
  323. ... class Meta:
  324. ... model = Category
  325. ... fields = ['slug', 'url', 'name']
  326. ... exclude = ['url']
  327. >>> CategoryForm.base_fields.keys()
  328. ['slug', 'name']
  329. # Old form_for_x tests #######################################################
  330. >>> from django.forms import ModelForm, CharField
  331. >>> import datetime
  332. >>> Category.objects.all()
  333. []
  334. >>> class CategoryForm(ModelForm):
  335. ... class Meta:
  336. ... model = Category
  337. >>> f = CategoryForm()
  338. >>> print f
  339. <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
  340. <tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr>
  341. <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
  342. >>> print f.as_ul()
  343. <li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="20" /></li>
  344. <li><label for="id_slug">Slug:</label> <input id="id_slug" type="text" name="slug" maxlength="20" /></li>
  345. <li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li>
  346. >>> print f['name']
  347. <input id="id_name" type="text" name="name" maxlength="20" />
  348. >>> f = CategoryForm(auto_id=False)
  349. >>> print f.as_ul()
  350. <li>Name: <input type="text" name="name" maxlength="20" /></li>
  351. <li>Slug: <input type="text" name="slug" maxlength="20" /></li>
  352. <li>The URL: <input type="text" name="url" maxlength="40" /></li>
  353. >>> f = CategoryForm({'name': 'Entertainment', 'slug': 'entertainment', 'url': 'entertainment'})
  354. >>> f.is_valid()
  355. True
  356. >>> f.cleaned_data['url']
  357. u'entertainment'
  358. >>> f.cleaned_data['name']
  359. u'Entertainment'
  360. >>> f.cleaned_data['slug']
  361. u'entertainment'
  362. >>> c1 = f.save()
  363. >>> c1
  364. <Category: Entertainment>
  365. >>> Category.objects.all()
  366. [<Category: Entertainment>]
  367. >>> f = CategoryForm({'name': "It's a test", 'slug': 'its-test', 'url': 'test'})
  368. >>> f.is_valid()
  369. True
  370. >>> f.cleaned_data['url']
  371. u'test'
  372. >>> f.cleaned_data['name']
  373. u"It's a test"
  374. >>> f.cleaned_data['slug']
  375. u'its-test'
  376. >>> c2 = f.save()
  377. >>> c2
  378. <Category: It's a test>
  379. >>> Category.objects.order_by('name')
  380. [<Category: Entertainment>, <Category: It's a test>]
  381. If you call save() with commit=False, then it will return an object that
  382. hasn't yet been saved to the database. In this case, it's up to you to call
  383. save() on the resulting model instance.
  384. >>> f = CategoryForm({'name': 'Third test', 'slug': 'third-test', 'url': 'third'})
  385. >>> f.is_valid()
  386. True
  387. >>> f.cleaned_data['url']
  388. u'third'
  389. >>> f.cleaned_data['name']
  390. u'Third test'
  391. >>> f.cleaned_data['slug']
  392. u'third-test'
  393. >>> c3 = f.save(commit=False)
  394. >>> c3
  395. <Category: Third test>
  396. >>> Category.objects.order_by('name')
  397. [<Category: Entertainment>, <Category: It's a test>]
  398. >>> c3.save()
  399. >>> Category.objects.order_by('name')
  400. [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>]
  401. If you call save() with invalid data, you'll get a ValueError.
  402. >>> f = CategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'})
  403. >>> f.errors['name']
  404. [u'This field is required.']
  405. >>> f.errors['slug']
  406. [u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]
  407. >>> f.cleaned_data
  408. Traceback (most recent call last):
  409. ...
  410. AttributeError: 'CategoryForm' object has no attribute 'cleaned_data'
  411. >>> f.save()
  412. Traceback (most recent call last):
  413. ...
  414. ValueError: The Category could not be created because the data didn't validate.
  415. >>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'})
  416. >>> f.save()
  417. Traceback (most recent call last):
  418. ...
  419. ValueError: The Category could not be created because the data didn't validate.
  420. Create a couple of Writers.
  421. >>> w_royko = Writer(name='Mike Royko')
  422. >>> w_royko.save()
  423. >>> w_woodward = Writer(name='Bob Woodward')
  424. >>> w_woodward.save()
  425. ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
  426. fields with the 'choices' attribute are represented by a ChoiceField.
  427. >>> class ArticleForm(ModelForm):
  428. ... class Meta:
  429. ... model = Article
  430. >>> f = ArticleForm(auto_id=False)
  431. >>> print f
  432. <tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
  433. <tr><th>Slug:</th><td><input type="text" name="slug" maxlength="50" /></td></tr>
  434. <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
  435. <tr><th>Writer:</th><td><select name="writer">
  436. <option value="" selected="selected">---------</option>
  437. <option value="...">Bob Woodward</option>
  438. <option value="...">Mike Royko</option>
  439. </select></td></tr>
  440. <tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
  441. <tr><th>Status:</th><td><select name="status">
  442. <option value="" selected="selected">---------</option>
  443. <option value="1">Draft</option>
  444. <option value="2">Pending</option>
  445. <option value="3">Live</option>
  446. </select></td></tr>
  447. <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
  448. <option value="...">Entertainment</option>
  449. <option value="...">It&#39;s a test</option>
  450. <option value="...">Third test</option>
  451. </select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
  452. You can restrict a form to a subset of the complete list of fields
  453. by providing a 'fields' argument. If you try to save a
  454. model created with such a form, you need to ensure that the fields
  455. that are _not_ on the form have default values, or are allowed to have
  456. a value of None. If a field isn't specified on a form, the object created
  457. from the form can't provide a value for that field!
  458. >>> class PartialArticleForm(ModelForm):
  459. ... class Meta:
  460. ... model = Article
  461. ... fields = ('headline','pub_date')
  462. >>> f = PartialArticleForm(auto_id=False)
  463. >>> print f
  464. <tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
  465. <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
  466. When the ModelForm is passed an instance, that instance's current values are
  467. inserted as 'initial' data in each Field.
  468. >>> w = Writer.objects.get(name='Mike Royko')
  469. >>> class RoykoForm(ModelForm):
  470. ... class Meta:
  471. ... model = Writer
  472. >>> f = RoykoForm(auto_id=False, instance=w)
  473. >>> print f
  474. <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br /><span class="helptext">Use both first and last names.</span></td></tr>
  475. >>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
  476. >>> art.save()
  477. >>> art_id_1 = art.id
  478. >>> art_id_1 is not None
  479. True
  480. >>> class TestArticleForm(ModelForm):
  481. ... class Meta:
  482. ... model = Article
  483. >>> f = TestArticleForm(auto_id=False, instance=art)
  484. >>> print f.as_ul()
  485. <li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li>
  486. <li>Slug: <input type="text" name="slug" value="test-article" maxlength="50" /></li>
  487. <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
  488. <li>Writer: <select name="writer">
  489. <option value="">---------</option>
  490. <option value="...">Bob Woodward</option>
  491. <option value="..." selected="selected">Mike Royko</option>
  492. </select></li>
  493. <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
  494. <li>Status: <select name="status">
  495. <option value="" selected="selected">---------</option>
  496. <option value="1">Draft</option>
  497. <option value="2">Pending</option>
  498. <option value="3">Live</option>
  499. </select></li>
  500. <li>Categories: <select multiple="multiple" name="categories">
  501. <option value="...">Entertainment</option>
  502. <option value="...">It&#39;s a test</option>
  503. <option value="...">Third test</option>
  504. </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
  505. >>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': unicode(w_royko.pk), 'article': 'Hello.'}, instance=art)
  506. >>> f.errors
  507. {}
  508. >>> f.is_valid()
  509. True
  510. >>> test_art = f.save()
  511. >>> test_art.id == art_id_1
  512. True
  513. >>> test_art = Article.objects.get(id=art_id_1)
  514. >>> test_art.headline
  515. u'Test headline'
  516. You can create a form over a subset of the available fields
  517. by specifying a 'fields' argument to form_for_instance.
  518. >>> class PartialArticleForm(ModelForm):
  519. ... class Meta:
  520. ... model = Article
  521. ... fields=('headline', 'slug', 'pub_date')
  522. >>> f = PartialArticleForm({'headline': u'New headline', 'slug': 'new-headline', 'pub_date': u'1988-01-04'}, auto_id=False, instance=art)
  523. >>> print f.as_ul()
  524. <li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li>
  525. <li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" /></li>
  526. <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
  527. >>> f.is_valid()
  528. True
  529. >>> new_art = f.save()
  530. >>> new_art.id == art_id_1
  531. True
  532. >>> new_art = Article.objects.get(id=art_id_1)
  533. >>> new_art.headline
  534. u'New headline'
  535. Add some categories and test the many-to-many form output.
  536. >>> new_art.categories.all()
  537. []
  538. >>> new_art.categories.add(Category.objects.get(name='Entertainment'))
  539. >>> new_art.categories.all()
  540. [<Category: Entertainment>]
  541. >>> class TestArticleForm(ModelForm):
  542. ... class Meta:
  543. ... model = Article
  544. >>> f = TestArticleForm(auto_id=False, instance=new_art)
  545. >>> print f.as_ul()
  546. <li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li>
  547. <li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" /></li>
  548. <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
  549. <li>Writer: <select name="writer">
  550. <option value="">---------</option>
  551. <option value="...">Bob Woodward</option>
  552. <option value="..." selected="selected">Mike Royko</option>
  553. </select></li>
  554. <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
  555. <li>Status: <select name="status">
  556. <option value="" selected="selected">---------</option>
  557. <option value="1">Draft</option>
  558. <option value="2">Pending</option>
  559. <option value="3">Live</option>
  560. </select></li>
  561. <li>Categories: <select multiple="multiple" name="categories">
  562. <option value="..." selected="selected">Entertainment</option>
  563. <option value="...">It&#39;s a test</option>
  564. <option value="...">Third test</option>
  565. </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
  566. Initial values can be provided for model forms
  567. >>> f = TestArticleForm(auto_id=False, initial={'headline': 'Your headline here', 'categories': [str(c1.id), str(c2.id)]})
  568. >>> print f.as_ul()
  569. <li>Headline: <input type="text" name="headline" value="Your headline here" maxlength="50" /></li>
  570. <li>Slug: <input type="text" name="slug" maxlength="50" /></li>
  571. <li>Pub date: <input type="text" name="pub_date" /></li>
  572. <li>Writer: <select name="writer">
  573. <option value="" selected="selected">---------</option>
  574. <option value="...">Bob Woodward</option>
  575. <option value="...">Mike Royko</option>
  576. </select></li>
  577. <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  578. <li>Status: <select name="status">
  579. <option value="" selected="selected">---------</option>
  580. <option value="1">Draft</option>
  581. <option value="2">Pending</option>
  582. <option value="3">Live</option>
  583. </select></li>
  584. <li>Categories: <select multiple="multiple" name="categories">
  585. <option value="..." selected="selected">Entertainment</option>
  586. <option value="..." selected="selected">It&#39;s a test</option>
  587. <option value="...">Third test</option>
  588. </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
  589. >>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
  590. ... 'writer': unicode(w_royko.pk), 'article': u'Hello.', 'categories': [unicode(c1.id), unicode(c2.id)]}, instance=new_art)
  591. >>> new_art = f.save()
  592. >>> new_art.id == art_id_1
  593. True
  594. >>> new_art = Article.objects.get(id=art_id_1)
  595. >>> new_art.categories.order_by('name')
  596. [<Category: Entertainment>, <Category: It's a test>]
  597. Now, submit form data with no categories. This deletes the existing categories.
  598. >>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
  599. ... 'writer': unicode(w_royko.pk), 'article': u'Hello.'}, instance=new_art)
  600. >>> new_art = f.save()
  601. >>> new_art.id == art_id_1
  602. True
  603. >>> new_art = Article.objects.get(id=art_id_1)
  604. >>> new_art.categories.all()
  605. []
  606. Create a new article, with categories, via the form.
  607. >>> class ArticleForm(ModelForm):
  608. ... class Meta:
  609. ... model = Article
  610. >>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01',
  611. ... 'writer': unicode(w_royko.pk), 'article': u'Test.', 'categories': [unicode(c1.id), unicode(c2.id)]})
  612. >>> new_art = f.save()
  613. >>> art_id_2 = new_art.id
  614. >>> art_id_2 not in (None, art_id_1)
  615. True
  616. >>> new_art = Article.objects.get(id=art_id_2)
  617. >>> new_art.categories.order_by('name')
  618. [<Category: Entertainment>, <Category: It's a test>]
  619. Create a new article, with no categories, via the form.
  620. >>> class ArticleForm(ModelForm):
  621. ... class Meta:
  622. ... model = Article
  623. >>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01',
  624. ... 'writer': unicode(w_royko.pk), 'article': u'Test.'})
  625. >>> new_art = f.save()
  626. >>> art_id_3 = new_art.id
  627. >>> art_id_3 not in (None, art_id_1, art_id_2)
  628. True
  629. >>> new_art = Article.objects.get(id=art_id_3)
  630. >>> new_art.categories.all()
  631. []
  632. Create a new article, with categories, via the form, but use commit=False.
  633. The m2m data won't be saved until save_m2m() is invoked on the form.
  634. >>> class ArticleForm(ModelForm):
  635. ... class Meta:
  636. ... model = Article
  637. >>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': u'1967-11-01',
  638. ... 'writer': unicode(w_royko.pk), 'article': u'Test.', 'categories': [unicode(c1.id), unicode(c2.id)]})
  639. >>> new_art = f.save(commit=False)
  640. # Manually save the instance
  641. >>> new_art.save()
  642. >>> art_id_4 = new_art.id
  643. >>> art_id_4 not in (None, art_id_1, art_id_2, art_id_3)
  644. True
  645. # The instance doesn't have m2m data yet
  646. >>> new_art = Article.objects.get(id=art_id_4)
  647. >>> new_art.categories.all()
  648. []
  649. # Save the m2m data on the form
  650. >>> f.save_m2m()
  651. >>> new_art.categories.order_by('name')
  652. [<Category: Entertainment>, <Category: It's a test>]
  653. Here, we define a custom ModelForm. Because it happens to have the same fields as
  654. the Category model, we can just call the form's save() to apply its changes to an
  655. existing Category instance.
  656. >>> class ShortCategory(ModelForm):
  657. ... name = CharField(max_length=5)
  658. ... slug = CharField(max_length=5)
  659. ... url = CharField(max_length=3)
  660. >>> cat = Category.objects.get(name='Third test')
  661. >>> cat
  662. <Category: Third test>
  663. >>> cat.id == c3.id
  664. True
  665. >>> form = ShortCategory({'name': 'Third', 'slug': 'third', 'url': '3rd'}, instance=cat)
  666. >>> form.save()
  667. <Category: Third>
  668. >>> Category.objects.get(id=c3.id)
  669. <Category: Third>
  670. Here, we demonstrate that choices for a ForeignKey ChoiceField are determined
  671. at runtime, based on the data in the database when the form is displayed, not
  672. the data in the database when the form is instantiated.
  673. >>> class ArticleForm(ModelForm):
  674. ... class Meta:
  675. ... model = Article
  676. >>> f = ArticleForm(auto_id=False)
  677. >>> print f.as_ul()
  678. <li>Headline: <input type="text" name="headline" maxlength="50" /></li>
  679. <li>Slug: <input type="text" name="slug" maxlength="50" /></li>
  680. <li>Pub date: <input type="text" name="pub_date" /></li>
  681. <li>Writer: <select name="writer">
  682. <option value="" selected="selected">---------</option>
  683. <option value="...">Bob Woodward</option>
  684. <option value="...">Mike Royko</option>
  685. </select></li>
  686. <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  687. <li>Status: <select name="status">
  688. <option value="" selected="selected">---------</option>
  689. <option value="1">Draft</option>
  690. <option value="2">Pending</option>
  691. <option value="3">Live</option>
  692. </select></li>
  693. <li>Categories: <select multiple="multiple" name="categories">
  694. <option value="...">Entertainment</option>
  695. <option value="...">It&#39;s a test</option>
  696. <option value="...">Third</option>
  697. </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
  698. >>> c4 = Category.objects.create(name='Fourth', url='4th')
  699. >>> c4
  700. <Category: Fourth>
  701. >>> Writer.objects.create(name='Carl Bernstein')
  702. <Writer: Carl Bernstein>
  703. >>> print f.as_ul()
  704. <li>Headline: <input type="text" name="headline" maxlength="50" /></li>
  705. <li>Slug: <input type="text" name="slug" maxlength="50" /></li>
  706. <li>Pub date: <input type="text" name="pub_date" /></li>
  707. <li>Writer: <select name="writer">
  708. <option value="" selected="selected">---------</option>
  709. <option value="...">Bob Woodward</option>
  710. <option value="...">Carl Bernstein</option>
  711. <option value="...">Mike Royko</option>
  712. </select></li>
  713. <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  714. <li>Status: <select name="status">
  715. <option value="" selected="selected">---------</option>
  716. <option value="1">Draft</option>
  717. <option value="2">Pending</option>
  718. <option value="3">Live</option>
  719. </select></li>
  720. <li>Categories: <select multiple="multiple" name="categories">
  721. <option value="...">Entertainment</option>
  722. <option value="...">It&#39;s a test</option>
  723. <option value="...">Third</option>
  724. <option value="...">Fourth</option>
  725. </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
  726. # ModelChoiceField ############################################################
  727. >>> from django.forms import ModelChoiceField, ModelMultipleChoiceField
  728. >>> f = ModelChoiceField(Category.objects.all())
  729. >>> list(f.choices)
  730. [(u'', u'---------'), (..., u'Entertainment'), (..., u"It's a test"), (..., u'Third'), (..., u'Fourth')]
  731. >>> f.clean('')
  732. Traceback (most recent call last):
  733. ...
  734. ValidationError: [u'This field is required.']
  735. >>> f.clean(None)
  736. Traceback (most recent call last):
  737. ...
  738. ValidationError: [u'This field is required.']
  739. >>> f.clean(0)
  740. Traceback (most recent call last):
  741. ...
  742. ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
  743. >>> f.clean(c3.id)
  744. <Category: Third>
  745. >>> f.clean(c2.id)
  746. <Category: It's a test>
  747. # Add a Category object *after* the ModelChoiceField has already been
  748. # instantiated. This proves clean() checks the database during clean() rather
  749. # than caching it at time of instantiation.
  750. >>> c5 = Category.objects.create(name='Fifth', url='5th')
  751. >>> c5
  752. <Category: Fifth>
  753. >>> f.clean(c5.id)
  754. <Category: Fifth>
  755. # Delete a Category object *after* the ModelChoiceField has already been
  756. # instantiated. This proves clean() checks the database during clean() rather
  757. # than caching it at time of instantiation.
  758. >>> Category.objects.get(url='5th').delete()
  759. >>> f.clean(c5.id)
  760. Traceback (most recent call last):
  761. ...
  762. ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
  763. >>> f = ModelChoiceField(Category.objects.filter(pk=c1.id), required=False)
  764. >>> print f.clean('')
  765. None
  766. >>> f.clean('')
  767. >>> f.clean(str(c1.id))
  768. <Category: Entertainment>
  769. >>> f.clean('100')
  770. Traceback (most recent call last):
  771. ...
  772. ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
  773. # queryset can be changed after the field is created.
  774. >>> f.queryset = Category.objects.exclude(name='Fourth')
  775. >>> list(f.choices)
  776. [(u'', u'---------'), (..., u'Entertainment'), (..., u"It's a test"), (..., u'Third')]
  777. >>> f.clean(c3.id)
  778. <Category: Third>
  779. >>> f.clean(c4.id)
  780. Traceback (most recent call last):
  781. ...
  782. ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
  783. # check that we can safely iterate choices repeatedly
  784. >>> gen_one = list(f.choices)
  785. >>> gen_two = f.choices
  786. >>> gen_one[2]
  787. (..., u"It's a test")
  788. >>> list(gen_two)
  789. [(u'', u'---------'), (..., u'Entertainment'), (..., u"It's a test"), (..., u'Third')]
  790. # check that we can override the label_from_instance method to print custom labels (#4620)
  791. >>> f.queryset = Category.objects.all()
  792. >>> f.label_from_instance = lambda obj: "category " + str(obj)
  793. >>> list(f.choices)
  794. [(u'', u'---------'), (..., 'category Entertainment'), (..., "category It's a test"), (..., 'category Third'), (..., 'category Fourth')]
  795. # ModelMultipleChoiceField ####################################################
  796. >>> f = ModelMultipleChoiceField(Category.objects.all())
  797. >>> list(f.choices)
  798. [(..., u'Entertainment'), (..., u"It's a test"), (..., u'Third'), (..., u'Fourth')]
  799. >>> f.clean(None)
  800. Traceback (most recent call last):
  801. ...
  802. ValidationError: [u'This field is required.']
  803. >>> f.clean([])
  804. Traceback (most recent call last):
  805. ...
  806. ValidationError: [u'This field is required.']
  807. >>> f.clean([c1.id])
  808. [<Category: Entertainment>]
  809. >>> f.clean([c2.id])
  810. [<Category: It's a test>]
  811. >>> f.clean([str(c1.id)])
  812. [<Category: Entertainment>]
  813. >>> f.clean([str(c1.id), str(c2.id)])
  814. [<Category: Entertainment>, <Category: It's a test>]
  815. >>> f.clean([c1.id, str(c2.id)])
  816. [<Category: Entertainment>, <Category: It's a test>]
  817. >>> f.clean((c1.id, str(c2.id)))
  818. [<Category: Entertainment>, <Category: It's a test>]
  819. >>> f.clean(['100'])
  820. Traceback (most recent call last):
  821. ...
  822. ValidationError: [u'Select a valid choice. 100 is not one of the available choices.']
  823. >>> f.clean('hello')
  824. Traceback (most recent call last):
  825. ...
  826. ValidationError: [u'Enter a list of values.']
  827. >>> f.clean(['fail'])
  828. Traceback (most recent call last):
  829. ...
  830. ValidationError: [u'"fail" is not a valid value for a primary key.']
  831. # Add a Category object *after* the ModelMultipleChoiceField has already been
  832. # instantiated. This proves clean() checks the database during clean() rather
  833. # than caching it at time of instantiation.
  834. >>> c6 = Category.objects.create(id=6, name='Sixth', url='6th')
  835. >>> c6
  836. <Category: Sixth>
  837. >>> f.clean([c6.id])
  838. [<Category: Sixth>]
  839. # Delete a Category object *after* the ModelMultipleChoiceField has already been
  840. # instantiated. This proves clean() checks the database during clean() rather
  841. # than caching it at time of instantiation.
  842. >>> Category.objects.get(url='6th').delete()
  843. >>> f.clean([c6.id])
  844. Traceback (most recent call last):
  845. ...
  846. ValidationError: [u'Select a valid choice. 6 is not one of the available choices.']
  847. >>> f = ModelMultipleChoiceField(Category.objects.all(), required=False)
  848. >>> f.clean([])
  849. []
  850. >>> f.clean(())
  851. []
  852. >>> f.clean(['10'])
  853. Traceback (most recent call last):
  854. ...
  855. ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
  856. >>> f.clean([str(c3.id), '10'])
  857. Traceback (most recent call last):
  858. ...
  859. ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
  860. >>> f.clean([str(c1.id), '10'])
  861. Traceback (most recent call last):
  862. ...
  863. ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
  864. # queryset can be changed after the field is created.
  865. >>> f.queryset = Category.objects.exclude(name='Fourth')
  866. >>> list(f.choices)
  867. [(..., u'Entertainment'), (..., u"It's a test"), (..., u'Third')]
  868. >>> f.clean([c3.id])
  869. [<Category: Third>]
  870. >>> f.clean([c4.id])
  871. Traceback (most recent call last):
  872. ...
  873. ValidationError: [u'Select a valid choice. ... is not one of the available choices.']
  874. >>> f.clean([str(c3.id), str(c4.id)])
  875. Traceback (most recent call last):
  876. ...
  877. ValidationError: [u'Select a valid choice. ... is not one of the available choices.']
  878. >>> f.queryset = Category.objects.all()
  879. >>> f.label_from_instance = lambda obj: "multicategory " + str(obj)
  880. >>> list(f.choices)
  881. [(..., 'multicategory Entertainment'), (..., "multicategory It's a test"), (..., 'multicategory Third'), (..., 'multicategory Fourth')]
  882. # OneToOneField ###############################################################
  883. >>> class ImprovedArticleForm(ModelForm):
  884. ... class Meta:
  885. ... model = ImprovedArticle
  886. >>> ImprovedArticleForm.base_fields.keys()
  887. ['article']
  888. >>> class ImprovedArticleWithParentLinkForm(ModelForm):
  889. ... class Meta:
  890. ... model = ImprovedArticleWithParentLink
  891. >>> ImprovedArticleWithParentLinkForm.base_fields.keys()
  892. []
  893. >>> bw = BetterWriter(name=u'Joe Better', score=10)
  894. >>> bw.save()
  895. >>> sorted(model_to_dict(bw).keys())
  896. ['id', 'name', 'score', 'writer_ptr']
  897. >>> class BetterWriterForm(ModelForm):
  898. ... class Meta:
  899. ... model = BetterWriter
  900. >>> form = BetterWriterForm({'name': 'Some Name', 'score': 12})
  901. >>> form.is_valid()
  902. True
  903. >>> bw2 = form.save()
  904. >>> bw2.delete()
  905. >>> class WriterProfileForm(ModelForm):
  906. ... class Meta:
  907. ... model = WriterProfile
  908. >>> form = WriterProfileForm()
  909. >>> print form.as_p()
  910. <p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
  911. <option value="" selected="selected">---------</option>
  912. <option value="...">Bob Woodward</option>
  913. <option value="...">Carl Bernstein</option>
  914. <option value="...">Joe Better</option>
  915. <option value="...">Mike Royko</option>
  916. </select></p>
  917. <p><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>
  918. >>> data = {
  919. ... 'writer': unicode(w_woodward.pk),
  920. ... 'age': u'65',
  921. ... }
  922. >>> form = WriterProfileForm(data)
  923. >>> instance = form.save()
  924. >>> instance
  925. <WriterProfile: Bob Woodward is 65>
  926. >>> form = WriterProfileForm(instance=instance)
  927. >>> print form.as_p()
  928. <p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
  929. <option value="">---------</option>
  930. <option value="..." selected="selected">Bob Woodward</option>
  931. <option value="...">Carl Bernstein</option>
  932. <option value="...">Joe Better</option>
  933. <option value="...">Mike Royko</option>
  934. </select></p>
  935. <p><label for="id_age">Age:</label> <input type="text" name="age" value="65" id="id_age" /></p>
  936. # PhoneNumberField ############################################################
  937. >>> class PhoneNumberForm(ModelForm):
  938. ... class Meta:
  939. ... model = PhoneNumber
  940. >>> f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'})
  941. >>> f.is_valid()
  942. True
  943. >>> f.cleaned_data['phone']
  944. u'312-555-1212'
  945. >>> f.cleaned_data['description']
  946. u'Assistance'
  947. # FileField ###################################################################
  948. # File forms.
  949. >>> class TextFileForm(ModelForm):
  950. ... class Meta:
  951. ... model = TextFile
  952. # Test conditions when files is either not given or empty.
  953. >>> f = TextFileForm(data={'description': u'Assistance'})
  954. >>> f.is_valid()
  955. False
  956. >>> f = TextFileForm(data={'description': u'Assistance'}, files={})
  957. >>> f.is_valid()
  958. False
  959. # Upload a file and ensure it all works as expected.
  960. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test1.txt', 'hello world')})
  961. >>> f.is_valid()
  962. True
  963. >>> type(f.cleaned_data['file'])
  964. <class 'django.core.files.uploadedfile.SimpleUploadedFile'>
  965. >>> instance = f.save()
  966. >>> instance.file
  967. <FieldFile: tests/test1.txt>
  968. >>> instance.file.delete()
  969. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test1.txt', 'hello world')})
  970. >>> f.is_valid()
  971. True
  972. >>> type(f.cleaned_data['file'])
  973. <class 'django.core.files.uploadedfile.SimpleUploadedFile'>
  974. >>> instance = f.save()
  975. >>> instance.file
  976. <FieldFile: tests/test1.txt>
  977. # Check if the max_length attribute has been inherited from the model.
  978. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test-maxlength.txt', 'hello world')})
  979. >>> f.is_valid()
  980. False
  981. # Edit an instance that already has the file defined in the model. This will not
  982. # save the file again, but leave it exactly as it is.
  983. >>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance)
  984. >>> f.is_valid()
  985. True
  986. >>> f.cleaned_data['file']
  987. <FieldFile: tests/test1.txt>
  988. >>> instance = f.save()
  989. >>> instance.file
  990. <FieldFile: tests/test1.txt>
  991. # Delete the current file since this is not done by Django.
  992. >>> instance.file.delete()
  993. # Override the file by uploading a new one.
  994. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test2.txt', 'hello world')}, instance=instance)
  995. >>> f.is_valid()
  996. True
  997. >>> instance = f.save()
  998. >>> instance.file
  999. <FieldFile: tests/test2.txt>
  1000. # Delete the current file since this is not done by Django.
  1001. >>> instance.file.delete()
  1002. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test2.txt', 'hello world')})
  1003. >>> f.is_valid()
  1004. True
  1005. >>> instance = f.save()
  1006. >>> instance.file
  1007. <FieldFile: tests/test2.txt>
  1008. # Delete the current file since this is not done by Django.
  1009. >>> instance.file.delete()
  1010. >>> instance.delete()
  1011. # Test the non-required FileField
  1012. >>> f = TextFileForm(data={'description': u'Assistance'})
  1013. >>> f.fields['file'].required = False
  1014. >>> f.is_valid()
  1015. True
  1016. >>> instance = f.save()
  1017. >>> instance.file
  1018. <FieldFile: None>
  1019. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test3.txt', 'hello world')}, instance=instance)
  1020. >>> f.is_valid()
  1021. True
  1022. >>> instance = f.save()
  1023. >>> instance.file
  1024. <FieldFile: tests/test3.txt>
  1025. # Instance can be edited w/out re-uploading the file and existing file should be preserved.
  1026. >>> f = TextFileForm(data={'description': u'New Description'}, instance=instance)
  1027. >>> f.fields['file'].required = False
  1028. >>> f.is_valid()
  1029. True
  1030. >>> instance = f.save()
  1031. >>> instance.description
  1032. u'New Description'
  1033. >>> instance.file
  1034. <FieldFile: tests/test3.txt>
  1035. # Delete the current file since this is not done by Django.
  1036. >>> instance.file.delete()
  1037. >>> instance.delete()
  1038. >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': SimpleUploadedFile('test3.txt', 'hello world')})
  1039. >>> f.is_valid()
  1040. True
  1041. >>> instance = f.save()
  1042. >>> instance.file
  1043. <FieldFile: tests/test3.txt>
  1044. # Delete the current file since this is not done by Django.
  1045. >>> instance.file.delete()
  1046. >>> instance.delete()
  1047. # BigIntegerField ################################################################
  1048. >>> class BigIntForm(forms.ModelForm):
  1049. ... class Meta:
  1050. ... model = BigInt
  1051. ...
  1052. >>> bif = BigIntForm({'biggie': '-9223372036854775808'})
  1053. >>> bif.is_valid()
  1054. True
  1055. >>> bif = BigIntForm({'biggie': '-9223372036854775809'})
  1056. >>> bif.is_valid()
  1057. False
  1058. >>> bif.errors
  1059. {'biggie': [u'Ensure this value is greater than or equal to -9223372036854775808.']}
  1060. >>> bif = BigIntForm({'biggie': '9223372036854775807'})
  1061. >>> bif.is_valid()
  1062. True
  1063. >>> bif = BigIntForm({'biggie': '9223372036854775808'})
  1064. >>> bif.is_valid()
  1065. False
  1066. >>> bif.errors
  1067. {'biggie': [u'Ensure this value is less than or equal to 9223372036854775807.']}
  1068. """}
  1069. if test_images:
  1070. __test__['API_TESTS'] += """
  1071. # ImageField ###################################################################
  1072. # ImageField and FileField are nearly identical, but they differ slighty when
  1073. # it comes to validation. This specifically tests that #6302 is fixed for
  1074. # both file fields and image fields.
  1075. >>> class ImageFileForm(ModelForm):
  1076. ... class Meta:
  1077. ... model = ImageFile
  1078. >>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
  1079. >>> image_data2 = open(os.path.join(os.path.dirname(__file__), "test2.png"), 'rb').read()
  1080. >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)})
  1081. >>> f.is_valid()
  1082. True
  1083. >>> type(f.cleaned_data['image'])
  1084. <class 'django.core.files.uploadedfile.SimpleUploadedFile'>
  1085. >>> instance = f.save()
  1086. >>> instance.image
  1087. <...FieldFile: tests/test.png>
  1088. >>> instance.width
  1089. 16
  1090. >>> instance.height
  1091. 16
  1092. # Delete the current file since this is not done by Django, but don't save
  1093. # because the dimension fields are not null=True.
  1094. >>> instance.image.delete(save=False)
  1095. >>> f = ImageFileForm(data={'description': u'An image'}, files={'image': SimpleUploadedFile('test.png', image_data)})
  1096. >>> f.is_valid()
  1097. True
  1098. >>> type(f.cleaned_data['image'])
  1099. <class 'django.core.files.uploadedfile.SimpleUploadedFile'>
  1100. >>> instance = f.save()
  1101. >>> instance.image
  1102. <...FieldFile: tests/test.png>
  1103. >>> instance.width
  1104. 16
  1105. >>> instance.height
  1106. 16
  1107. # Edit an instance that already has the (required) image defined in the model. This will not
  1108. # save the image again, but leave it exactly as it is.
  1109. >>> f = ImageFileForm(data={'description': u'Look, it changed'}, instance=instance)
  1110. >>> f.is_valid()
  1111. True
  1112. >>> f.cleaned_data['image']
  1113. <...FieldFile: tests/test.png>
  1114. >>> instance = f.save()
  1115. >>> instance.image
  1116. <...FieldFile: tests/test.png>
  1117. >>> instance.height
  1118. 16
  1119. >>> instance.width
  1120. 16
  1121. # Delete the current file since this is not done by Django, but don't save
  1122. # because the dimension fields are not null=True.
  1123. >>> instance.image.delete(save=False)
  1124. # Override the file by uploading a new one.
  1125. >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)}, instance=instance)
  1126. >>> f.is_valid()
  1127. True
  1128. >>> instance = f.save()
  1129. >>> instance.image
  1130. <...FieldFile: tests/test2.png>
  1131. >>> instance.height
  1132. 32
  1133. >>> instance.width
  1134. 48
  1135. # Delete the current file since this is not done by Django, but don't save
  1136. # because the dimension fields are not null=True.
  1137. >>> instance.image.delete(save=False)
  1138. >>> instance.delete()
  1139. >>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': SimpleUploadedFile('test2.png', image_data2)})
  1140. >>> f.is_valid()
  1141. True
  1142. >>> instance = f.save()
  1143. >>> instance.image
  1144. <...FieldFile: tests/test2.png>
  1145. >>> instance.height
  1146. 32
  1147. >>> instance.width
  1148. 48
  1149. # Delete the current file since this is not done by Django, but don't save
  1150. # because the dimension fields are not null=True.
  1151. >>> instance.image.delete(save=False)
  1152. >>> instance.delete()
  1153. # Test the non-required ImageField
  1154. >>> class OptionalImageFileForm(ModelForm):
  1155. ... class Meta:
  1156. ... model = OptionalImageFile
  1157. >>> f = OptionalImageFileForm(data={'description': u'Test'})
  1158. >>> f.is_valid()
  1159. True
  1160. >>> instance = f.save()
  1161. >>> instance.image
  1162. <...FieldFile: None>
  1163. >>> instance.width
  1164. >>> instance.height
  1165. >>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test3.png', image_data)}, instance=instance)
  1166. >>> f.is_valid()
  1167. True
  1168. >>> instance = f.save()
  1169. >>> instance.image
  1170. <...FieldFile: tests/test3.png>
  1171. >>> instance.width
  1172. 16
  1173. >>> instance.height
  1174. 16
  1175. # Editing the instance without re-uploading the image should not affect the image or its width/height properties
  1176. >>> f = OptionalImageFileForm(data={'description': u'New Description'}, instance=instance)
  1177. >>> f.is_valid()
  1178. True
  1179. >>> instance = f.save()
  1180. >>> instance.description
  1181. u'New Description'
  1182. >>> instance.image
  1183. <...FieldFile: tests/test3.png>
  1184. >>> instance.width
  1185. 16
  1186. >>> instance.height
  1187. 16
  1188. # Delete the current file since this is not done by Django.
  1189. >>> instance.image.delete()
  1190. >>> instance.delete()
  1191. >>> f = OptionalImageFileForm(data={'description': u'And a final one'}, files={'image': SimpleUploadedFile('test4.png', image_data2)})
  1192. >>> f.is_valid()
  1193. True
  1194. >>> instance = f.save()
  1195. >>> instance.image
  1196. <...FieldFile: tests/test4.png>
  1197. >>> instance.width
  1198. 48
  1199. >>> instance.height
  1200. 32
  1201. >>> instance.delete()
  1202. # Test callable upload_to behavior that's dependent on the value of another field in the model
  1203. >>> f = ImageFileForm(data={'description': u'And a final one', 'path': 'foo'}, files={'image': SimpleUploadedFile('test4.png', image_data)})
  1204. >>> f.is_valid()
  1205. True
  1206. >>> instance = f.save()
  1207. >>> instance.image
  1208. <...FieldFile: foo/test4.png>
  1209. >>> instance.delete()
  1210. """
  1211. __test__['API_TESTS'] += """
  1212. # Media on a ModelForm ########################################################
  1213. # Similar to a regular Form class you can define custom media to be used on
  1214. # the ModelForm.
  1215. >>> class ModelFormWithMedia(ModelForm):
  1216. ... class Media:
  1217. ... js = ('/some/form/javascript',)
  1218. ... css = {
  1219. ... 'all': ('/some/form/css',)
  1220. ... }
  1221. ... class Meta:
  1222. ... model = PhoneNumber
  1223. >>> f = ModelFormWithMedia()
  1224. >>> print f.media
  1225. <link href="/some/form/css" type="text/css" media="all" rel="stylesheet" />
  1226. <script type="text/javascript" src="/some/form/javascript"></script>
  1227. >>> class CommaSeparatedIntegerForm(ModelForm):
  1228. ... class Meta:
  1229. ... model = CommaSeparatedInteger
  1230. >>> f = CommaSeparatedIntegerForm({'field': '1,2,3'})
  1231. >>> f.is_valid()
  1232. True
  1233. >>> f.cleaned_data
  1234. {'field': u'1,2,3'}
  1235. >>> f = CommaSeparatedIntegerForm({'field': '1a,2'})
  1236. >>> f.errors
  1237. {'field': [u'Enter only digits separated by commas.']}
  1238. >>> f = CommaSeparatedIntegerForm({'field': ',,,,'})
  1239. >>> f.is_valid()
  1240. True
  1241. >>> f.cleaned_data
  1242. {'field': u',,,,'}
  1243. >>> f = CommaSeparatedIntegerForm({'field': '1.2'})
  1244. >>> f.errors
  1245. {'field': [u'Enter only digits separated by commas.']}
  1246. >>> f = CommaSeparatedIntegerForm({'field': '1,a,2'})
  1247. >>> f.errors
  1248. {'field': [u'Enter only digits separated by commas.']}
  1249. >>> f = CommaSeparatedIntegerForm({'field': '1,,2'})
  1250. >>> f.is_valid()
  1251. True
  1252. >>> f.cleaned_data
  1253. {'field': u'1,,2'}
  1254. >>> f = CommaSeparatedIntegerForm({'field': '1'})
  1255. >>> f.is_valid()
  1256. True
  1257. >>> f.cleaned_data
  1258. {'field': u'1'}
  1259. This Price instance generated by this form is not valid because the quantity
  1260. field is required, but the form is valid because the field is excluded from
  1261. the form. This is for backwards compatibility.
  1262. >>> class PriceForm(ModelForm):
  1263. ... class Meta:
  1264. ... model = Price
  1265. ... exclude = ('quantity',)
  1266. >>> form = PriceForm({'price': '6.00'})
  1267. >>> form.is_valid()
  1268. True
  1269. >>> price = form.save(commit=False)
  1270. >>> price.full_clean()
  1271. Traceback (most recent call last):
  1272. ...
  1273. ValidationError: {'quantity': [u'This field cannot be null.']}
  1274. The form should not validate fields that it doesn't contain even if they are
  1275. specified using 'fields', not 'exclude'.
  1276. ... class Meta:
  1277. ... model = Price
  1278. ... fields = ('price',)
  1279. >>> form = PriceForm({'price': '6.00'})
  1280. >>> form.is_valid()
  1281. True
  1282. The form should still have an instance of a model that is not complete and
  1283. not saved into a DB yet.
  1284. >>> form.instance.price
  1285. Decimal('6.00')
  1286. >>> form.instance.quantity is None
  1287. True
  1288. >>> form.instance.pk is None
  1289. True
  1290. # Choices on CharField and IntegerField
  1291. >>> class ArticleForm(ModelForm):
  1292. ... class Meta:
  1293. ... model = Article
  1294. >>> f = ArticleForm()
  1295. >>> f.fields['status'].clean('42')
  1296. Traceback (most recent call last):
  1297. ...
  1298. ValidationError: [u'Select a valid choice. 42 is not one of the available choices.']
  1299. >>> class ArticleStatusForm(ModelForm):
  1300. ... class Meta:
  1301. ... model = ArticleStatus
  1302. >>> f = ArticleStatusForm()
  1303. >>> f.fields['status'].clean('z')
  1304. Traceback (most recent call last):
  1305. ...
  1306. ValidationError: [u'Select a valid choice. z is not one of the available choices.']
  1307. # Foreign keys which use to_field #############################################
  1308. >>> apple = Inventory.objects.create(barcode=86, name='Apple')
  1309. >>> pear = Inventory.objects.create(barcode=22, name='Pear')
  1310. >>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
  1311. >>> field = ModelChoiceField(Inventory.objects.all(), to_field_name='barcode')
  1312. >>> for choice in field.choices:
  1313. ... print choice
  1314. (u'', u'---------')
  1315. (86, u'Apple')
  1316. (87, u'Core')
  1317. (22, u'Pear')
  1318. >>> class InventoryForm(ModelForm):
  1319. ... class Meta:
  1320. ... model = Inventory
  1321. >>> form = InventoryForm(instance=core)
  1322. >>> print form['parent']
  1323. <select name="parent" id="id_parent">
  1324. <option value="">---------</option>
  1325. <option value="86" selected="selected">Apple</option>
  1326. <option value="87">Core</option>
  1327. <option value="22">Pear</option>
  1328. </select>
  1329. >>> data = model_to_dict(core)
  1330. >>> data['parent'] = '22'
  1331. >>> form = InventoryForm(data=data, instance=core)
  1332. >>> core = form.save()
  1333. >>> core.parent
  1334. <Inventory: Pear>
  1335. >>> class CategoryForm(ModelForm):
  1336. ... description = forms.CharField()
  1337. ... class Meta:
  1338. ... model = Category
  1339. ... fields = ['description', 'url']
  1340. >>> CategoryForm.base_fields.keys()
  1341. ['description', 'url']
  1342. >>> print CategoryForm()
  1343. <tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr>
  1344. <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
  1345. # to_field_name should also work on ModelMultipleChoiceField ##################
  1346. >>> field = ModelMultipleChoiceField(Inventory.objects.all(), to_field_name='barcode')
  1347. >>> for choice in field.choices:
  1348. ... print choice
  1349. (86, u'Apple')
  1350. (87, u'Core')
  1351. (22, u'Pear')
  1352. >>> field.clean([86])
  1353. [<Inventory: Apple>]
  1354. >>> class SelectInventoryForm(forms.Form):
  1355. ... items = ModelMultipleChoiceField(Inventory.objects.all(), to_field_name='barcode')
  1356. >>> form = SelectInventoryForm({'items': [87, 22]})
  1357. >>> form.is_valid()
  1358. True
  1359. >>> form.cleaned_data
  1360. {'items': [<Inventory: Core>, <Inventory: Pear>]}
  1361. # Model field that returns None to exclude itself with explicit fields ########
  1362. >>> class CustomFieldForExclusionForm(ModelForm):
  1363. ... class Meta:
  1364. ... model = CustomFieldForExclusionModel
  1365. ... fields = ['name', 'markup']
  1366. >>> CustomFieldForExclusionForm.base_fields.keys()
  1367. ['name']
  1368. >>> print CustomFieldForExclusionForm()
  1369. <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="10" /></td></tr>
  1370. # Clean up
  1371. >>> import shutil
  1372. >>> shutil.rmtree(temp_storage_dir)
  1373. """