Django

Code

Changeset 1511

Show
Ignore:
Timestamp:
11/30/05 21:06:30 (3 years ago)
Author:
jacob
Message:

Fixed #757: manually set AutoField? values are now respected; also added unit test to verify the correct behavior. Thanks, cygnus.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r1509 r1511  
    8888    ye7cakf02@sneakemail.com 
    8989 
    90  
    9190A big THANK YOU goes to: 
    9291 
  • django/trunk/django/core/meta/__init__.py

    r1508 r1511  
    10101010    if not pk_set or not record_exists: 
    10111011        field_names = [db.db.quote_name(f.column) for f in opts.fields if not isinstance(f, AutoField)] 
     1012        db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), True)) for f in opts.fields if not isinstance(f, AutoField)] 
     1013        # If the PK has been manually set we must respect that 
     1014        if pk_set: 
     1015            field_names += [f.column for f in opts.fields if isinstance(f, AutoField)] 
     1016            db_values += [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in opts.fields if isinstance(f, AutoField)] 
    10121017        placeholders = ['%s'] * len(field_names) 
    1013         db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), True)) for f in opts.fields if not isinstance(f, AutoField)] 
    10141018        if opts.order_with_respect_to: 
    10151019            field_names.append(db.db.quote_name('_order')) 
     
    10211025            (db.db.quote_name(opts.db_table), ','.join(field_names), 
    10221026            ','.join(placeholders)), db_values) 
    1023         if opts.has_auto_field
     1027        if opts.has_auto_field and not pk_set
    10241028            setattr(self, opts.pk.attname, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column)) 
    10251029    db.db.commit() 
  • django/trunk/tests/testapp/models/basic.py

    r1033 r1511  
    158158>>> a8.id 
    1591598L 
     160 
     161# You can manually specify the primary key when creating a new objet 
     162>>> a101 = articles.Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45)) 
     163>>> a101.save() 
     164>>> a101 = articles.get_object(pk=101) 
     165>>> a101.headline 
     166'Article 101' 
     167 
    160168""" 
    161169