#499 closed (fixed)
[patch] method_save() empty value fix
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | new-admin |
Severity: | major | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I've found a bug in django.core.meta in function method_save which would create SQL statementes with an empty value such as name= when updating. Those queries would fail (postgresql and mysql). Here is a patch. It fixed my problem but I haven't tested it very thoroughly so typical discalimers apply :). Probably can be done better so feel free to improve.
Great software, I'm so happy to have an excuse to improve my Python skills....
*** django/core/meta/__init__.py.orig 2005-09-13 17:34:54.591722120 +0200 --- django/core/meta/__init__.py 2005-09-13 17:31:55.037018584 +0200 *************** *** 782,787 **** --- 782,793 ---- # 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]) else:
Change History (4)
comment:1 by , 19 years ago
comment:2 by , 19 years ago
I think I've found the real fix here. http://code.djangoproject.com/browser/django/branches/new-admin/django/core/meta/fields.py#l641
It adds a db_prep_save method to ForeignKeys. If this happens with other fields, please post about it here , they need a db_prep_save method.
Basically, empty values from POST were just being shoved in as strings rather than None which ends up being NULL in the db.
comment:3 by , 19 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
This should be fixed in the new-admin merge.
comment:4 by , 18 years ago
Type: | enhancement |
---|
This also occured in my project.
The situation is an update form for an item that has edit_inline=meta.TABULAR for a field.
This means that field gets no form field (which I'm not sure is good), but is still included in non_pks.
I'm not sure how this works in the admin view.
I'm not sure about this fix. I think the right fix is to make sure that only fields that are on the page get put in the non_pks array. This means defining what effect all the edit_inline stuff has, because at the moment it is extremely vague.