| 1 | |
| 2 | import datetime |
| 3 | from django.db import models |
| 4 | |
| 5 | class Blog(models.Model): |
| 6 | title = models.CharField(max_length=100) |
| 7 | is_active = models.BooleanField() |
| 8 | |
| 9 | def __unicode__(self): |
| 10 | return self.title |
| 11 | |
| 12 | class Post(models.Model): |
| 13 | blog = models.ForeignKey(Blog) |
| 14 | title = models.CharField(max_length=100) |
| 15 | pub_date = models.DateTimeField(default=datetime.datetime.now) |
| 16 | |
| 17 | def __unicode__(self): |
| 18 | return self.title |
| 19 | |
| 20 | __test__ = {'API_TESTS': """ |
| 21 | >>> from django.newforms import formset_for_model, formset_for_queryset, inline_formset |
| 22 | |
| 23 | >>> Blog.objects.all() |
| 24 | [] |
| 25 | |
| 26 | >>> BlogFormSet = formset_for_queryset(Blog.objects.all()) |
| 27 | >>> formset = BlogFormSet() |
| 28 | >>> for form in formset.forms: |
| 29 | ... print form |
| 30 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input id="id_form-0-title" type="text" name="form-0-title" maxlength="100" /></td></tr> |
| 31 | <tr><th><label for="id_form-0-is_active">Is active:</label></th><td><input type="checkbox" name="form-0-is_active" id="id_form-0-is_active" /></td></tr> |
| 32 | <tr><th><label for="id_form-0-DELETE">Delete:</label></th><td><input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr> |
| 33 | |
| 34 | Create an actual blog object. |
| 35 | |
| 36 | >>> b1 = Blog(title=u'My First Blog') |
| 37 | >>> b1.save() |
| 38 | |
| 39 | >>> BlogFormSet = formset_for_queryset(Blog.objects.all()) |
| 40 | >>> formset = BlogFormSet() |
| 41 | >>> for form in formset.forms: |
| 42 | ... print form |
| 43 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input id="id_form-0-title" type="text" name="form-0-title" value="My First Blog" maxlength="100" /></td></tr> |
| 44 | <tr><th><label for="id_form-0-is_active">Is active:</label></th><td><input type="checkbox" name="form-0-is_active" id="id_form-0-is_active" /></td></tr> |
| 45 | <tr><th><label for="id_form-0-DELETE">Delete:</label></th><td><input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /><input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></td></tr> |
| 46 | <tr><th><label for="id_form-1-title">Title:</label></th><td><input id="id_form-1-title" type="text" name="form-1-title" maxlength="100" /></td></tr> |
| 47 | <tr><th><label for="id_form-1-is_active">Is active:</label></th><td><input type="checkbox" name="form-1-is_active" id="id_form-1-is_active" /></td></tr> |
| 48 | <tr><th><label for="id_form-1-DELETE">Delete:</label></th><td><input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /><input type="hidden" name="form-1-id" id="id_form-1-id" /></td></tr> |
| 49 | |
| 50 | Now POST some data and see if the instance was updated correctly. |
| 51 | |
| 52 | >>> data = { |
| 53 | ... 'form-COUNT': '2', |
| 54 | ... 'form-0-id': '1', |
| 55 | ... 'form-0-title': 'My First Blog', |
| 56 | ... 'form-0-DELETE': '', |
| 57 | ... 'form-1-id': '', |
| 58 | ... 'form-1-title': 'My Second Blog', |
| 59 | ... 'form-1-DELETE': '', |
| 60 | ... } |
| 61 | >>> formset = BlogFormSet(data) |
| 62 | >>> formset.is_valid() |
| 63 | True |
| 64 | >>> formset.save() |
| 65 | [<Blog: My First Blog>, <Blog: My Second Blog>] |
| 66 | |
| 67 | Now lets create one post in each blog. |
| 68 | |
| 69 | >>> b1.post_set.add(Post(title=u'How to Pour a Guinness')) |
| 70 | >>> b2 = Blog.objects.get(pk=2) |
| 71 | >>> b2.post_set.add(Post(title=u'Bad Pigs! Why Your Bacon Tastes Bad.')) |
| 72 | |
| 73 | >>> PostFormSet = inline_formset(Blog, Post, extra=1) |
| 74 | >>> formset = PostFormSet(b2) |
| 75 | >>> for form in formset.forms: |
| 76 | ... print form |
| 77 | <tr><th><label for="id_post_set-0-title">Title:</label></th><td><input id="id_post_set-0-title" type="text" name="post_set-0-title" value="Bad Pigs! Why Your Bacon Tastes Bad." maxlength="100" /></td></tr> |
| 78 | <tr><th><label for="id_post_set-0-pub_date">Pub date:</label></th><td><input type="text" name="post_set-0-pub_date" value="2007-10-11 00:42:31.209597" id="id_post_set-0-pub_date" /></td></tr> |
| 79 | <tr><th><label for="id_post_set-0-DELETE">Delete:</label></th><td><input type="checkbox" name="post_set-0-DELETE" id="id_post_set-0-DELETE" /><input type="hidden" name="post_set-0-id" value="2" id="id_post_set-0-id" /></td></tr> |
| 80 | <tr><th><label for="id_post_set-1-title">Title:</label></th><td><input id="id_post_set-1-title" type="text" name="post_set-1-title" maxlength="100" /></td></tr> |
| 81 | <tr><th><label for="id_post_set-1-pub_date">Pub date:</label></th><td><input type="text" name="post_set-1-pub_date" id="id_post_set-1-pub_date" /></td></tr> |
| 82 | <tr><th><label for="id_post_set-1-DELETE">Delete:</label></th><td><input type="checkbox" name="post_set-1-DELETE" id="id_post_set-1-DELETE" /><input type="hidden" name="post_set-1-id" id="id_post_set-1-id" /></td></tr> |
| 83 | """} |