1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

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
This commit is contained in:
Adrian Holovaty
2006-12-30 00:12:02 +00:00
parent 0919920bc0
commit b9fdf9abb8
2 changed files with 31 additions and 2 deletions

View File

@@ -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):