From b9fdf9abb841f1eda11fb4d8094372dbbbeb58cb Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sat, 30 Dec 2006 00:12:02 +0000 Subject: [PATCH] newforms: Got form_for_instance() to select initial ManyToManyField values properly git-svn-id: http://code.djangoproject.com/svn/django/trunk@4261 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 4 ++++ tests/modeltests/model_forms/models.py | 29 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 433b5bde13..c4159d88fd 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -726,6 +726,10 @@ class ManyToManyField(RelatedField, Field): return getattr(obj, self.attname).all() def formfield(self, initial=None): + # If initial is passed in, it's a list of related objects, but the + # MultipleChoiceField takes a list of IDs. + if initial is not None: + initial = [i._get_pk_val() for i in initial] return forms.MultipleChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial) class ManyToOneRel(object): diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 60a3defde5..f9cbce996f 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -28,7 +28,7 @@ class Writer(models.Model): class Article(models.Model): headline = models.CharField(maxlength=50) - pub_date = models.DateTimeField() + pub_date = models.DateField() writer = models.ForeignKey(Writer) categories = models.ManyToManyField(Category, blank=True) @@ -80,6 +80,8 @@ __test__ = {'API_TESTS': """ >>> Category.objects.all() [, ] +If you call create() with save=False, then it will return an object that hasn't +yet been saved. In this case, it's up to you to save it. >>> f = CategoryForm({'name': 'Third test', 'url': 'third'}) >>> f.errors {} @@ -94,6 +96,7 @@ __test__ = {'API_TESTS': """ >>> Category.objects.all() [, , ] +If you call create() with invalid data, you'll get a ValueError. >>> f = CategoryForm({'name': '', 'url': 'foo'}) >>> f.errors {'name': [u'This field is required.']} @@ -102,7 +105,6 @@ __test__ = {'API_TESTS': """ Traceback (most recent call last): ... ValueError: The Category could not be created because the data didn't validate. - >>> f = CategoryForm({'name': '', 'url': 'foo'}) >>> f.create() Traceback (most recent call last): @@ -181,4 +183,27 @@ True >>> new_art = Article.objects.get(id=1) >>> new_art.headline 'New headline' + +Add some categories and test the many-to-many form output. +>>> new_art.categories.all() +[] +>>> new_art.categories.add(Category.objects.get(name='Entertainment')) +>>> new_art.categories.all() +[] +>>> TestArticleForm = form_for_instance(new_art) +>>> f = TestArticleForm(auto_id=False) +>>> print f.as_ul() +
  • Headline:
  • +
  • Pub date:
  • +
  • Writer:
  • +
  • Categories:
  • + """}