Ticket #14234: dj14234-save-base_tests.diff

File dj14234-save-base_tests.diff, 2.0 KB (added by David Reynolds, 14 years ago)

Patch with tests included

  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 6077f56..aee136b 100644
    a b class Model(object):  
    558558        if origin and not meta.auto_created:
    559559            signals.post_save.send(sender=origin, instance=self,
    560560                created=(not record_exists), raw=raw, using=using)
    561 
     561        self._adding = False
    562562    save_base.alters_data = True
    563563
    564564    def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
  • tests/regressiontests/forms/models.py

    diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
    index a4891df..d2c6c24 100644
    a b class Group(models.Model):  
    7272    def __unicode__(self):
    7373        return u'%s' % self.name
    7474
     75class Ticket14234(models.Model):
     76   name = models.CharField(max_length=100)
     77   age = models.IntegerField()
     78   description = models.TextField(blank=True)
     79
     80   def __unicode__(self):
     81       return self.name
     82
     83   def save(self, *args, **kwargs):
     84       self.full_clean()
     85       return super(Ticket14234, self).save(*args, **kwargs)
     86
     87class Ticket14234Form(django_forms.ModelForm):
     88   class Meta:
     89       model = Ticket14234
     90
     91class TestTicket14234(TestCase):
     92    ''' ModelForm.instance._adding should be set to False after ModelForm.save is executed (test for #14234)'''
     93    def test_things(self):
     94        form = Ticket14234Form({
     95            'name': 'Fred',
     96            'age': '56',
     97            'description': 'Old Fred is 56',
     98        })
     99        if form.is_valid():
     100            self.assertEqual(form.instance._adding, True)
     101            obj = form.save()
     102            self.assertEqual(obj._adding, False)
     103            obj.age = 27
     104            obj.save()  # Without the patch we cannot resave
     105
    75106class TestTicket12510(TestCase):
    76107    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
    77108    def setUp(self):
Back to Top