Ticket #757: pk_save.diff

File pk_save.diff, 1.6 KB (added by cygnus@…, 19 years ago)

Patch to make saving primary key values work.

  • django/core/meta/__init__.py

     
    796796            record_exists = False
    797797    if not pk_set or not record_exists:
    798798        field_names = [f.column for f in opts.fields if not isinstance(f, AutoField)]
     799        db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in opts.fields if not isinstance(f, AutoField)]
     800
     801        if pk_set:
     802            field_names += [f.column for f in opts.fields if isinstance(f, AutoField)]
     803            db_values += [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in opts.fields if isinstance(f, AutoField)]
     804
    799805        placeholders = ['%s'] * len(field_names)
    800         db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in opts.fields if not isinstance(f, AutoField)]
     806
    801807        if opts.order_with_respect_to:
    802808            field_names.append('_order')
    803809            # TODO: This assumes the database supports subqueries.
     
    806812            db_values.append(getattr(self, opts.order_with_respect_to.column))
    807813        cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % (opts.db_table,
    808814            ','.join(field_names), ','.join(placeholders)), db_values)
    809         if opts.has_auto_field:
     815        if opts.has_auto_field and not pk_set:
    810816            setattr(self, opts.pk.column, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column))
    811817    db.db.commit()
    812818    # Run any post-save hooks.
Back to Top