Ticket #10672: 10672.diff

File 10672.diff, 5.2 KB (added by k0001, 16 years ago)
  • django/db/models/base.py

     
    416416        override this method. It's separate from save() in order to hide the
    417417        need for overrides of save() to pass around internal-only parameters
    418418        ('raw' and 'cls').
     419
     420        Returns True if the instance has been inserted, otherwise False if it
     421        has been updated.
    419422        """
    420423        assert not (force_insert and force_update)
    421424        if not cls:
     
    439442                if field and getattr(self, parent._meta.pk.attname) is None and getattr(self, field.attname) is not None:
    440443                    setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
    441444
    442                 self.save_base(cls=parent)
     445                record_exists = not self.save_base(cls=parent)
    443446                if field:
    444447                    setattr(self, field.attname, self._get_pk_val(parent._meta))
    445             if meta.proxy:
    446                 return
    447448
    448449        if not meta.proxy:
    449450            non_pks = [f for f in meta.local_fields if not f.primary_key]
     
    494495            signals.post_save.send(sender=self.__class__, instance=self,
    495496                created=(not record_exists), raw=raw)
    496497
     498        return (not record_exists)
     499
    497500    save_base.alters_data = True
    498501
    499502    def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
  • tests/modeltests/force_insert_update/models.py

     
    6060...
    6161DatabaseError: ...
    6262
     63# Model's internal save_base() method should return True on insert and False on update
     64>>> cc3 = Counter(name=u"three", value=3)
     65>>> cc3.save_base()
     66True
     67>>> cc3.name = u"THREE"
     68>>> cc3.save_base()
     69False
     70>>> cc3.name = u"Thr33"
     71>>> cc3.save_base(force_update=True)
     72False
     73>>> cc4 = Counter(name=u"four", value=4)
     74>>> cc4.save_base(force_insert=True)
     75True
    6376"""
    6477}
  • tests/modeltests/proxy_models/models.py

     
    171171[<OtherPerson: barney>, <OtherPerson: fred>]
    172172>>> OtherPerson._default_manager.all()
    173173[<OtherPerson: barney>, <OtherPerson: wilma>]
     174
     175
     176# Test save signals for proxy models
     177>>> from django.db.models import signals
     178>>> def foo(*args, **kwargs): print u"Foo"
     179...
     180>>> def bar(*args, **kwargs): print u"Bar"
     181...
     182>>> signals.pre_save.connect(foo, sender=MyPerson)
     183>>> signals.post_save.connect(bar, sender=MyPerson)
     184>>> dino = MyPerson.objects.create(name=u"dino")
     185Foo
     186Bar
    174187"""}
    175188
    176189
  • tests/modeltests/signals/models.py

     
    6363post_save signal, Tom Smith
    6464Is updated
    6565Is raw
     66False
    6667
    6768>>> p1.delete()
    6869pre_delete signal, Tom Smith
  • tests/regressiontests/model_inheritance_regress/models.py

     
    121121# Create a child-parent-grandparent chain
    122122>>> place1 = Place(name="Guido's House of Pasta", address='944 W. Fullerton')
    123123>>> place1.save_base(raw=True)
     124True
    124125>>> restaurant = Restaurant(place_ptr=place1, serves_hot_dogs=True, serves_pizza=False)
    125126>>> restaurant.save_base(raw=True)
     127True
    126128>>> italian_restaurant = ItalianRestaurant(restaurant_ptr=restaurant, serves_gnocchi=True)
    127129>>> italian_restaurant.save_base(raw=True)
     130True
    128131
    129132# Create a child-parent chain with an explicit parent link
    130133>>> place2 = Place(name='Main St', address='111 Main St')
    131134>>> place2.save_base(raw=True)
     135True
    132136>>> park = ParkingLot(parent=place2, capacity=100)
    133137>>> park.save_base(raw=True)
     138True
    134139
    135140# Check that no extra parent objects have been created.
    136141>>> Place.objects.all()
     
    148153>>> [sorted(d.items()) for d in dicts]
    149154[[('capacity', 100), ('name', u'Main St')]]
    150155
    151 # You can also update objects when using a raw save.
     156# You can also update objects when using a raw save, in which case False is returned.
    152157>>> place1.name = "Guido's All New House of Pasta"
    153158>>> place1.save_base(raw=True)
     159False
    154160
    155161>>> restaurant.serves_hot_dogs = False
    156162>>> restaurant.save_base(raw=True)
     163False
    157164
    158165>>> italian_restaurant.serves_gnocchi = False
    159166>>> italian_restaurant.save_base(raw=True)
     167False
    160168
    161169>>> place2.name='Derelict lot'
    162170>>> place2.save_base(raw=True)
     171False
    163172
    164173>>> park.capacity = 50
    165174>>> park.save_base(raw=True)
     175False
    166176
    167177# No extra parent objects after an update, either.
    168178>>> Place.objects.all()
     
    185195
    186196>>> italian_restaurant.name = "Lorenzo's Pasta Hut"
    187197>>> italian_restaurant.save_base(raw=True)
     198False
    188199
    189200# Note that the name has not changed
    190201# - name is an attribute of Place, not ItalianRestaurant
Back to Top