summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBrian Rosner <brosner@gmail.com>2008-07-15 22:41:17 +0000
committerBrian Rosner <brosner@gmail.com>2008-07-15 22:41:17 +0000
commit55744e997f92f24608910eecf60509603ec87fec (patch)
tree84de0937813db909e11856c853a172625892b31b /tests
parenta2c7bfc1bee48b2aed9124c1a5a59a72274c73f7 (diff)
newforms-admin: Fixed #7230 -- Added a save_m2m method to BaseModelFormSet when commit=False is passed to save. Thanks Books Travis for the original report.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7930 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/model_formsets/models.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py
index 93519a4421..84265450fc 100644
--- a/tests/modeltests/model_formsets/models.py
+++ b/tests/modeltests/model_formsets/models.py
@@ -13,9 +13,19 @@ class Book(models.Model):
def __unicode__(self):
return self.title
+class AuthorMeeting(models.Model):
+ name = models.CharField(max_length=100)
+ authors = models.ManyToManyField(Author)
+ created = models.DateField(editable=False)
+
+ def __unicode__(self):
+ return self.name
+
__test__ = {'API_TESTS': """
+>>> from datetime import date
+
>>> from django.newforms.models import modelformset_factory
>>> qs = Author.objects.all()
@@ -162,6 +172,40 @@ True
>>> formset.save()
[<Author: Walt Whitman>]
+Test the behavior of commit=False and save_m2m
+
+>>> meeting = AuthorMeeting.objects.create(created=date.today())
+>>> meeting.authors = Author.objects.all()
+
+# create an Author instance to add to the meeting.
+>>> new_author = Author.objects.create(name=u'John Steinbeck')
+
+>>> AuthorMeetingFormSet = modelformset_factory(AuthorMeeting, extra=1, can_delete=True)
+>>> data = {
+... 'form-TOTAL_FORMS': '2', # the number of forms rendered
+... 'form-INITIAL_FORMS': '1', # the number of forms with initial data
+... 'form-MAX_FORMS': '0', # the max number of forms
+... 'form-0-id': '1',
+... 'form-0-name': '2nd Tuesday of the Week Meeting',
+... 'form-0-authors': [2, 1, 3, 4],
+... 'form-1-name': '',
+... 'form-1-authors': '',
+... 'form-1-DELETE': '',
+... }
+>>> formset = AuthorMeetingFormSet(data=data, queryset=AuthorMeeting.objects.all())
+>>> formset.is_valid()
+True
+>>> instances = formset.save(commit=False)
+>>> for instance in instances:
+... instance.created = date.today()
+... instance.save()
+>>> formset.save_m2m()
+>>> instances[0].authors.all()
+[<Author: Charles Baudelaire>, <Author: Walt Whitman>, <Author: Paul Verlaine>, <Author: John Steinbeck>]
+
+# delete the author we created to allow later tests to continue working.
+>>> new_author.delete()
+
Test the behavior of max_num with model formsets. It should properly limit
the queryset to reduce the amount of objects being pulled in when not being
used.