diff --git a/django/contrib/admin/templates/admin/edit_inline_tabular.html b/django/contrib/admin/templates/admin/edit_inline_tabular.html index 5e51b3772b..4ccf61ddab 100644 --- a/django/contrib/admin/templates/admin/edit_inline_tabular.html +++ b/django/contrib/admin/templates/admin/edit_inline_tabular.html @@ -32,12 +32,12 @@ {% if fcw.obj.original %}View on site{% endif %} {% endif %} - + {% endfor %} - + {% for fcw in bound_related_object.form_field_collection_wrappers %} {% for bound_field in fcw.bound_fields %} diff --git a/django/contrib/admin/templatetags/admin_modify.py b/django/contrib/admin/templatetags/admin_modify.py index 7f8c8753e2..2ff6b25e0e 100644 --- a/django/contrib/admin/templatetags/admin_modify.py +++ b/django/contrib/admin/templatetags/admin_modify.py @@ -112,11 +112,12 @@ class FieldWrapper(object): and self.field.rel.raw_id_admin class FormFieldCollectionWrapper(object): - def __init__(self, field_mapping, fields): + def __init__(self, field_mapping, fields, index): self.field_mapping = field_mapping self.fields = fields self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original']) for field in self.fields] + self.index = index class TabularBoundRelatedObject(BoundRelatedObject): def __init__(self, related_object, field_mapping, original): @@ -124,9 +125,9 @@ class TabularBoundRelatedObject(BoundRelatedObject): self.field_wrapper_list = [FieldWrapper(field) for field in self.relation.editable_fields()] fields = self.relation.editable_fields() - - self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping, fields) - for field_mapping in self.field_mappings] + + self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping, fields, i) + for (i,field_mapping) in self.field_mappings.items() ] self.original_row_needed = max([fw.use_raw_id_admin() for fw in self.field_wrapper_list]) self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url') diff --git a/django/contrib/admin/views/stages/change.py b/django/contrib/admin/views/stages/change.py index 39eda8f3e9..361979c641 100644 --- a/django/contrib/admin/views/stages/change.py +++ b/django/contrib/admin/views/stages/change.py @@ -78,7 +78,6 @@ def change_stage(request, path, object_id): else: # Populate new_data with a "flattened" version of the current data. new_data = manipulator.flatten_data() - # TODO: do this in flatten_data... # If the object has ordered objects on its admin page, get the existing # order and flatten it into a comma-separated list of IDs. diff --git a/django/core/formfields.py b/django/core/formfields.py index e437acc2ca..4dd1800e3b 100644 --- a/django/core/formfields.py +++ b/django/core/formfields.py @@ -122,10 +122,10 @@ class FormWrapper: def fill_inline_collections(self): if not self._inline_collections: ic = [] - related_objects = self.manipulator.get_related_objects() - for rel_obj in related_objects: + children = self.manipulator.children.items() + for rel_obj, child_manips in children: data = rel_obj.extract_data(self.data) - inline_collection = InlineObjectCollection(self.manipulator, rel_obj, data, self.error_dict) + inline_collection = InlineObjectCollection(self.manipulator, rel_obj, child_manips, data, self.error_dict) ic.append(inline_collection) self._inline_collections = ic @@ -203,12 +203,13 @@ class FormFieldCollection(FormFieldWrapper): return ''.join([field.html_error_list() for field in self.formfield_dict.values() if hasattr(field, 'errors')]) class InlineObjectCollection: - "An object that acts like a list of form field collections." - def __init__(self, parent_manipulator, rel_obj, data, errors): + "An object that acts like a sparse list of form field collections." + def __init__(self, parent_manipulator, rel_obj,child_manips, data, errors): self.parent_manipulator = parent_manipulator self.rel_obj = rel_obj self.data = data self.errors = errors + self.child_manips = child_manips self._collections = None self.name = rel_obj.name @@ -230,27 +231,32 @@ class InlineObjectCollection: def __iter__(self): self.fill() - return self._collections.__iter__() + return self._collections.values().__iter__() + + def items(self): + self.fill() + return self._collections.items() def fill(self): if self._collections: return else: - var_name = self.rel_obj.opts.object_name.lower() - wrapper = [] - orig = hasattr(self.parent_manipulator, 'original_object') and self.parent_manipulator.original_object or None - orig_list = self.rel_obj.get_list(orig) - for i, instance in enumerate(orig_list): - collection = {'original': instance} - for f in self.rel_obj.editable_fields(): - for field_name in f.get_manipulator_field_names(''): - full_field_name = '%s.%d.%s' % (var_name, i, field_name) - field = self.parent_manipulator[full_field_name] - data = field.extract_data(self.data) - errors = self.errors.get(full_field_name, []) - collection[field_name] = FormFieldWrapper(field, data, errors) - wrapper.append(FormFieldCollection(collection)) - self._collections = wrapper + #var_name = self.rel_obj.opts.object_name.lower() + cols = {} + #orig = hasattr(self.parent_manipulator, 'original_object') and self.parent_manipulator.original_object or None + #orig_list = self.rel_obj.get_list(orig) + + for i, manip in enumerate(self.child_manips) : + if manip: + collection = {'original': manip.original_object} + for field in manip.fields: + errors = self.errors.get(field.field_name, []) + data = field.extract_data(self.data) + last_part = field.field_name[field.field_name.rindex('.') + 1:] + collection[last_part] = FormFieldWrapper(field, data, errors) + + cols[i] = FormFieldCollection(collection) + self._collections = cols class FormField: """Abstract class representing a form field. diff --git a/django/db/models/manipulators.py b/django/db/models/manipulators.py index 2ffd22e8c1..eada7aa721 100644 --- a/django/db/models/manipulators.py +++ b/django/db/models/manipulators.py @@ -42,20 +42,20 @@ class ManipulatorDescriptor(object): return self.man class ManipulatorHelper(object): - def __init__(self, manip, related_collection): + def __init__(self, manip, related, child_manipulators): self.manip = manip - self.related_collection = related_collection + self.related = related + self.child_manipulators = child_manipulators class FillHelper(ManipulatorHelper): def matched_item(self,index, child_manip, obj_data): child_manip._fill_data(obj_data) def missing_item(self, index, child_manip): - self.related_collection[index] = None + self.child_manipulators[index] = None - def new_item(self): - child_manip = self.manip.model.AddManipulator() - self.related_collection.append(child_manip) + def new_item(self, obj_data): + child_manip = self.manip._add_manipulator_for_child(self.related) child_manip._fill_data(obj_data) class SaveHelper(ManipulatorHelper): @@ -65,9 +65,11 @@ class SaveHelper(ManipulatorHelper): def missing_item(self, index, child_manip): child_manip.manip.original_object.delete(ignore_objects=[parent.original_object]) - def new_item(self, index, obj_data): - child_manip = self.manip.model.AddManipulator() - child_manip._save_expanded(obj_data) + def new_item(self, obj_data): + child_manip = self.manip._add_manipulator_for_child(self.related) + overrides = { self.related.field : + getattr(self.manip.original_object, self.manip.opts.pk.attname) } + child_manip._save_expanded(obj_data, overrides) class AutomaticManipulator(Manipulator): def _prepare(cls, model): @@ -118,16 +120,20 @@ class AutomaticManipulator(Manipulator): def get_original_value(self, field): raise NotImplementedError - def get_new_object(self, expanded_data): + def get_new_object(self, expanded_data, overrides=None): params = {} - + overrides = overrides or {} for f in self.opts.fields: - # Fields with auto_now_add should keep their original value in the change stage. - auto_now_add = self.change and getattr(f, 'auto_now_add', False) - if self.follow.get(f.name, None) and not auto_now_add: - param = f.get_manipulator_new_data(expanded_data) + over = overrides.get(f, None) + if over: + param = over else: - param = self.get_original_value(f) + # Fields with auto_now_add should keep their original value in the change stage. + auto_now_add = self.change and getattr(f, 'auto_now_add', False) + if self.follow.get(f.name, None) and not auto_now_add: + param = f.get_manipulator_new_data(expanded_data) + else: + param = self.get_original_value(f) params[f.attname] = param @@ -137,12 +143,12 @@ class AutomaticManipulator(Manipulator): def _fill_related_objects(self, expanded_data, helper_factory): for related, manips in self.children.items(): - helper = helper_factory(self, related) - child_data = expanded_data[related.var_name] + helper = helper_factory(self, related, manips) + child_data = MultiValueDict(expanded_data[related.var_name]) # existing objects for index,manip in enumerate(manips): obj_data = child_data.get(str(index), None) - child_data.pop(str(index) ) + child_data.pop(str(index), None) if obj_data != None: #the object has new data helper.matched_item(index,manip, obj_data ) @@ -151,7 +157,7 @@ class AutomaticManipulator(Manipulator): helper.missing_item(index,manip) if child_data: # There are new objects in the data - for index, obj_data in child_data: + for index, obj_data in child_data.items(): helper.new_item(obj_data) def _fill_data(self, expanded_data): @@ -168,7 +174,7 @@ class AutomaticManipulator(Manipulator): self._do_command_expanded(expanded_data, command_parts) def _do_command_expanded(self, expanded_data, command_parts): - try: + try: part = command_parts.pop(0) except IndexError: raise BadCommand, "Not enough parts in command" @@ -188,40 +194,49 @@ class AutomaticManipulator(Manipulator): if child_data == None: raise BadCommand, "'%s' : could not find data for manipulator collection." % (part,) - # The next part could be an index of a manipulator, - # or it could be a command on the collection. - try: - part = command_parts.pop(0) - except IndexError: - raise BadCommand, "Not enough parts in command" - try: - index = int(index_part) - manip = child_manips.get(index, None) - - if manip == None: - raise BadCommand, "No %s manipulator found for index %s in command." % (part, index) - obj_data = child_data.get(index_part, None) - if obj_data == None: - raise BadCommand, "Could not find data for manipulator %s in %s collection." % (index, part) - if command_parts == ["delete"]: - child_manips[index] = None - else: - manip._do_command_expanded(obj_data,command_parts) - except ValueError: - # Must be a command on the collection. Possible commands: - # add. - if index_part == "add": - child_manips.append(related.model.AddManipulator()) - + # The next part could be an index of a manipulator, + # or it could be a command on the collection. + try: + index_part = command_parts.pop(0) + except IndexError: + raise BadCommand, "Not enough parts in command" + try: + index = int(index_part) + manip = child_manips[index] + + if manip == None: + raise BadCommand, "No %s manipulator found for index %s in command." % (part, index) + obj_data = child_data.get(index_part, None) + if obj_data == None: + raise BadCommand, "Could not find data for manipulator %s in %s collection." % (index, part) + if command_parts == ["delete"]: + child_manips[index] = None + else: + manip._do_command_expanded(obj_data,command_parts) + except ValueError: + # Must be a command on the collection. Possible commands: + # add. + if index_part == "add": + self._add_manipulator_for_child(related) + else: + raise BadCommand, "%s, unknown command" % (part) + + def _add_manipulator_for_child(self, related): + child_manips = self.children.setdefault(related, []) + fol = self.follow[related.name] + prefix = "%s%s.%s." % (self.name_prefix, related.var_name, len(child_manips) ) + child_manip = related.model.AddManipulator(follow=fol,name_prefix=prefix) + child_manips.append(child_manip) + return child_manip def save(self, new_data): expanded_data = dot_expand(new_data,MultiValueDict) return self._save_expanded(expanded_data) - def _save_expanded(self, expanded_data): + def _save_expanded(self, expanded_data, overrides = None): add, change, opts, klass = self.add, self.change, self.opts, self.model - new_object = self.get_new_object(expanded_data) + new_object = self.get_new_object(expanded_data, overrides) # First, save the basic object itself. new_object.save() @@ -254,29 +269,6 @@ class AutomaticManipulator(Manipulator): self._fill_related_objects(expanded_data,SaveHelper) return new_object -# for related, manips in self.children.items(): -# child_data = expanded_data[related.var_name] -# #print "with child:", name -# # Apply changes to existing objects -# for index,manip in enumerate(manips): -# obj_data = child_data.get(str(index), None) -# child_data.pop(str(index) ) -# if obj_data != None: -# #save the object with the new data -# #print "saving child data:", obj_data -# manip._save_expanded(obj_data) -# else: -# #delete the object as it was not in the data -# manip.original_object.delete(ignore_objects=[self]) -# #print "deleting child object:", manip.original_original -# if child_data: -# # There are new objects in the data, so -# # add them. -# for index, obj_data in child_data: -# manip = related.model.AddManipulator() -# manip._save_expanded(obj_data) -# #print "new data to be added:", child_data - # Save the order, if applicable. #if change and opts.get_ordered_objects(): @@ -290,9 +282,17 @@ class AutomaticManipulator(Manipulator): def flatten_data(self): new_data = {} - for f in self.opts.get_data_holders(self.follow): - fol = self.follow.get(f.name) - new_data.update(f.flatten_data(fol, self.original_object)) + for f in self.opts.fields + self.opts.many_to_many: + fol = self.follow.get(f.name, None) + if fol: + new_data.update(f.flatten_data(fol, self.original_object)) + for rel, child_manips in self.children.items(): + for manip in child_manips: + if manip: + child_data = manip.flatten_data() + new_data.update(child_data) + + new_data = dict([(self.name_prefix +k, v) for k,v in new_data.items()]) return new_data class ModelAddManipulator(AutomaticManipulator): diff --git a/django/db/models/related.py b/django/db/models/related.py index 03b2737549..f24c0620fa 100644 --- a/django/db/models/related.py +++ b/django/db/models/related.py @@ -84,7 +84,8 @@ class RelatedObject(object): if parent_manipulator.original_object: meth_name = 'get_%s_list' % self.get_method_name_part() list = getattr(parent_manipulator.original_object, meth_name)() - + else: + list = [] manipulators = [] for i,obj in enumerate(list): prefix = '%s%s.%d.' % (parent_manipulator.name_prefix, self.var_name, i)