Changeset 5658
- Timestamp:
- 07/12/07 02:45:35 (1 year ago)
- Files:
-
- django/trunk/django/core/serializers/base.py (modified) (1 diff)
- django/trunk/django/db/models/base.py (modified) (3 diffs)
- django/trunk/docs/db-api.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/serializers_regress/models.py (modified) (1 diff)
- django/trunk/tests/regressiontests/serializers_regress/tests.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/serializers/base.py
r5609 r5658 159 159 160 160 def save(self, save_m2m=True): 161 self.object.save() 161 # Call save on the Model baseclass directly. This bypasses any 162 # model-defined save. The save is also forced to be raw. 163 # This ensures that the data that is deserialized is literally 164 # what came from the file, not post-processed by pre_save/save 165 # methods. 166 models.Model.save(self.object, raw=True) 162 167 if self.m2m_data and save_m2m: 163 168 for accessor_name, object_list in self.m2m_data.items(): django/trunk/django/db/models/base.py
r5609 r5658 202 202 _prepare = classmethod(_prepare) 203 203 204 def save(self ):204 def save(self, raw=False): 205 205 dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) 206 206 … … 219 219 # If it does already exist, do an UPDATE. 220 220 if cursor.fetchone()[0] > 0: 221 db_values = [f.get_db_prep_save( f.pre_save(self, False)) for f in non_pks]221 db_values = [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, False)) for f in non_pks] 222 222 if db_values: 223 223 cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \ … … 230 230 if not pk_set or not record_exists: 231 231 field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)] 232 db_values = [f.get_db_prep_save( f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]232 db_values = [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)] 233 233 # If the PK has been manually set, respect that. 234 234 if pk_set: 235 235 field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)] 236 db_values += [f.get_db_prep_save( f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]236 db_values += [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)] 237 237 placeholders = ['%s'] * len(field_names) 238 238 if self._meta.order_with_respect_to: django/trunk/docs/db-api.txt
r5609 r5658 118 118 Explicitly specifying auto-primary-key values is mostly useful for bulk-saving 119 119 objects, when you're confident you won't have primary-key collision. 120 121 Raw saves 122 --------- 123 124 When you save an Django object, some pre-processing will occur on the the data 125 that is in the object. For example, if your model has a ``DateField`` with 126 ``auto_now=True`` set, the pre-save phase will alter the data in the object 127 to ensure that the date field contains the current date stamp. 128 129 Although these automated changes can be very useful, there will be times when 130 you just want to save the data as-is. In these cases, you can invoke a *Raw Save* 131 by passing ``raw=True`` as an argument to the ``save()`` method:: 132 133 b4.save(raw=True) # Saves object, but does no pre-processing 134 135 A raw save saves all the data in your object, but performs no pre-save processing 136 on the data in the object. 120 137 121 138 Saving changes to objects django/trunk/tests/regressiontests/serializers_regress/models.py
r5409 r5658 210 210 field2 = models.CharField(maxlength=10) 211 211 field3 = models.CharField(maxlength=10) 212 213 # Tests for handling fields with pre_save functions, or 214 # models with save functions that modify data 215 class AutoNowDateTimeData(models.Model): 216 data = models.DateTimeField(null=True, auto_now=True) 217 218 class ModifyingSaveData(models.Model): 219 data = models.IntegerField(null=True) 220 221 def save(self): 222 "A save method that modifies the data in the object" 223 self.data = 666 224 super(ModifyingSaveData, self).save(raw) django/trunk/tests/regressiontests/serializers_regress/tests.py
r5609 r5658 25 25 26 26 # A set of functions that can be used to recreate 27 # test data objects of various kinds 27 # test data objects of various kinds. 28 # The save method is a raw base model save, to make 29 # sure that the data in the database matches the 30 # exact test case. 28 31 def data_create(pk, klass, data): 29 32 instance = klass(id=pk) 30 33 instance.data = data 31 instance.save()34 models.Model.save(instance, raw=True) 32 35 return instance 33 36 … … 35 38 instance = klass(id=pk) 36 39 instance.data = data[0] 37 instance.save()40 models.Model.save(instance, raw=True) 38 41 for tag in data[1:]: 39 42 instance.tags.create(data=tag) … … 43 46 instance = klass(id=pk) 44 47 setattr(instance, 'data_id', data) 45 instance.save()48 models.Model.save(instance, raw=True) 46 49 return instance 47 50 48 51 def m2m_create(pk, klass, data): 49 52 instance = klass(id=pk) 50 instance.save()53 models.Model.save(instance, raw=True) 51 54 instance.data = data 52 55 return instance … … 55 58 instance = klass() 56 59 instance.data_id = data 57 instance.save()60 models.Model.save(instance, raw=True) 58 61 return instance 59 62 … … 61 64 instance = klass() 62 65 instance.data = data 63 instance.save()66 models.Model.save(instance, raw=True) 64 67 return instance 65 68 … … 250 253 (pk_obj, 780, USStatePKData, "MA"), 251 254 # (pk_obj, 790, XMLPKData, "<foo></foo>"), 255 256 (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006,6,16,10,42,37)), 257 (data_obj, 810, ModifyingSaveData, 42), 252 258 ] 253 259 … … 304 310 305 311 obj = ComplexModel(field1='first',field2='second',field3='third') 306 obj.save( )312 obj.save(raw=True) 307 313 308 314 # Serialize then deserialize the test database … … 320 326 321 327 obj = ComplexModel(field1='first',field2='second',field3='third') 322 obj.save( )328 obj.save(raw=True) 323 329 324 330 # Serialize the test database to a stream
