From e6c080db2bdbeb102f65e57bbedb0aed362e4756 Mon Sep 17 00:00:00 2001 From: Robert Wittams Date: Fri, 30 Sep 2005 17:17:50 +0000 Subject: [PATCH] Possible fix for #499. Also gets error handling working properly in the admin without changing field names. git-svn-id: http://code.djangoproject.com/svn/django/branches/new-admin@748 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/formfields.py | 17 ++++++++++++----- django/core/meta/__init__.py | 6 ------ django/core/meta/fields.py | 8 +++++++- django/views/admin/main.py | 21 ++++++++++++--------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/django/core/formfields.py b/django/core/formfields.py index cae6aadb9a..bab592e0b8 100644 --- a/django/core/formfields.py +++ b/django/core/formfields.py @@ -300,12 +300,16 @@ class FormField: def convert_post_data(self, new_data): name = self.get_member_name() - if new_data.has_key(name): - d = new_data.getlist(name) + if new_data.has_key(self.field_name): + d = new_data.getlist(self.field_name) #del new_data[self.field_name] - new_data.setlist(name, - [self.__class__.html2python(data) - for data in d]) + try: + converted_data = [self.__class__.html2python(data) + for data in d] + except ValueError: + converted_data = d + new_data.setlist(name, converted_data) + else: try: # individual fields deal with None values themselves @@ -860,6 +864,9 @@ class CommaSeparatedIntegerField(TextField): except validators.ValidationError, e: raise validators.CriticalValidationError, e.messages + def html2python(data): + return data.split(','); + class XMLLargeTextField(LargeTextField): """ A LargeTextField with an XML validator. The schema_path argument is the diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index e2070ab22e..4c89841b3b 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -859,12 +859,6 @@ def method_save(opts, self): # If it does already exist, do an UPDATE. if cursor.fetchone(): db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.column), False)) for f in non_pks] - while 1: - try: - idx = db_values.index('') - non_pks[idx:idx+1] = [] - db_values[idx:idx +1] = [] - except: break cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % (opts.db_table, ','.join(['%s=%%s' % f.column for f in non_pks]), opts.pk.column), db_values + [pk_val]) diff --git a/django/core/meta/fields.py b/django/core/meta/fields.py index edc6093b67..464cba4a75 100644 --- a/django/core/meta/fields.py +++ b/django/core/meta/fields.py @@ -163,7 +163,7 @@ class Field(object): """ Returns a list of field names that this object adds to the manipulator. """ - return [name_prefix + self.column] + return [name_prefix + self.name] def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False): """ @@ -638,6 +638,12 @@ class ForeignKey(Field): def get_manipulator_field_objs(self): return [formfields.IntegerField] + def get_db_prep_save(self,value): + if value == '': + return None + else: + return int(value) + def flatten_data(self, obj = None): if not obj: # In required many-to-one fields with only one available choice, diff --git a/django/views/admin/main.py b/django/views/admin/main.py index 11102a63b4..9401254b8e 100644 --- a/django/views/admin/main.py +++ b/django/views/admin/main.py @@ -686,11 +686,12 @@ def add_stage_new(request, app_label, module_name, show_delete=False, form_url=' if opts.has_field_type(meta.FileField): new_data.update(request.FILES) errors = manipulator.get_validation_errors(new_data) + manipulator.do_html2python(new_data) + if not errors and not request.POST.has_key("_preview"): for f in opts.many_to_many: if f.rel.raw_id_admin: new_data.setlist(f.name, new_data[f.name].split(",")) - manipulator.do_html2python(new_data) new_object = manipulator.save(new_data) pk_value = getattr(new_object, opts.pk.column) log.log_action(request.user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.ADDITION) @@ -711,8 +712,8 @@ def add_stage_new(request, app_label, module_name, show_delete=False, form_url=' else: request.user.add_message(msg) return HttpResponseRedirect(post_url) - if request.POST.has_key("_preview"): - manipulator.do_html2python(new_data) + # if request.POST.has_key("_preview"): # Always happens anyway. + # manipulator.do_html2python(new_data) else: # Add default data. new_data = manipulator.flatten_data() @@ -757,11 +758,13 @@ def change_stage_new(request, app_label, module_name, object_id): new_data.update(request.FILES) errors = manipulator.get_validation_errors(new_data) + + manipulator.do_html2python(new_data) if not errors and not request.POST.has_key("_preview"): - for f in opts.many_to_many: - if f.rel.raw_id_admin: - new_data.setlist(f.name, new_data[f.name].split(",")) - manipulator.do_html2python(new_data) + # Now done in commaseparatedint + # for f in opts.many_to_many: + # if f.rel.raw_id_admin: + # new_data.setlist(f.name, new_data[f.name].split(",")) new_object = manipulator.save(new_data) pk_value = getattr(new_object, opts.pk.column) @@ -794,8 +797,8 @@ def change_stage_new(request, app_label, module_name, object_id): else: request.user.add_message(msg) return HttpResponseRedirect("../") - if request.POST.has_key("_preview"): - manipulator.do_html2python(new_data) + # if request.POST.has_key("_preview"): # always happens + # manipulator.do_html2python(new_data) else: # Populate new_data with a "flattened" version of the current data. new_data = manipulator.flatten_data()