magic-removal: Fixed #1372 -- Got many-to-many fields working in manipulators

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-02-23 20:10:41 +00:00
parent 208ea7bacb
commit f252b11a9c
2 changed files with 14 additions and 11 deletions

View File

@ -478,13 +478,13 @@ class ManyToManyField(RelatedField, Field):
def isValidIDList(self, field_data, all_data):
"Validates that the value is a valid list of foreign keys"
mod = self.rel.to._meta.get_model_module()
mod = self.rel.to
try:
pks = map(int, field_data.split(','))
except ValueError:
# the CommaSeparatedIntegerField validator will catch this error
return
objects = mod.get_in_bulk(pks)
objects = mod._default_manager.in_bulk(pks)
if len(objects) != len(pks):
badkeys = [k for k in pks if k not in objects]
raise validators.ValidationError, ngettext("Please enter valid %(self)s IDs. The value %(value)r is invalid.",

View File

@ -112,7 +112,7 @@ class AutomaticManipulator(forms.Manipulator):
if not f.primary_key and str(getattr(self.original_object, f.attname)) != str(getattr(new_object, f.attname)):
self.fields_changed.append(f.verbose_name)
# Save many-to-many objects. Example: Poll.set_sites()
# Save many-to-many objects. Example: Set sites for a poll.
for f in self.opts.many_to_many:
if self.follow.get(f.name, None):
if not f.rel.edit_inline:
@ -120,9 +120,12 @@ class AutomaticManipulator(forms.Manipulator):
new_vals = new_data.get(f.name, ())
else:
new_vals = new_data.getlist(f.name)
was_changed = getattr(new_object, 'set_%s' % f.name)(new_vals)
if self.change and was_changed:
self.fields_changed.append(f.verbose_name)
# First, clear the existing values.
rel_manager = getattr(new_object, f.name)
rel_manager.clear()
# Then, set the new values.
for n in new_vals:
rel_manager.add(f.rel.to._default_manager.get(pk=n))
expanded_data = DotExpandedDict(dict(new_data))
# Save many-to-one objects. Example: Add the Choice objects for a Poll.