Ticket #10733: deferred_related_fields_test.diff

File deferred_related_fields_test.diff, 4.1 KB (added by Andy Durdin, 16 years ago)

Test to replicate the bug

  • tests/regressiontests/model_regress/fixtures/deferred_related_objects.json

     
     1[
     2    {
     3        "pk": "1",
     4        "model": "model_regress.deferredrelateda",
     5        "fields": {
     6            "name": "a1",
     7            "lots_of_text": "Lots of text A",
     8            "a_field": "A goes here"
     9        }
     10    },
     11    {
     12        "pk": "2",
     13        "model": "model_regress.deferredrelatedb",
     14        "fields": {
     15            "name": "b2",
     16            "lots_of_text": "Lots of text B",
     17            "b_field": "B goes here"
     18        }
     19    },
     20    {
     21        "pk": "3",
     22        "model": "model_regress.deferredrelatedc",
     23        "fields": {
     24            "name": "c3",
     25            "lots_of_text": "Lots of text C",
     26            "a": 1,
     27            "b": 2,
     28            "is_published": 0
     29        }
     30    }
     31]
  • tests/regressiontests/model_regress/tests.py

     
     1from django.test import TestCase
     2
     3class DeferredRelatedModelTest(TestCase):
     4    fixtures = ["deferred_related_objects.json"]
     5
     6    def test_deferred_related_fields(self):
     7        from models import DeferredRelatedC
     8
     9        results = DeferredRelatedC.objects.all().only('name', 'a', 'b', 'a__name', 'b__name').select_related()
     10        self.assertEqual(results[0].a.name, u'a1')
     11        self.assertEqual(results[0].b.name, u'b2')
     12        self.assertEqual(results[0].a.lots_of_text, u'Lots of text A')
     13        self.assertEqual(results[0].b.lots_of_text, u'Lots of text B')
  • tests/regressiontests/model_regress/models.py

     
    5858        # object).
    5959        return 'Názov: %s' % self.name
    6060
     61class DeferredRelatedBase(models.Model):
     62    name = models.CharField(max_length=10)
     63    lots_of_text = models.TextField()
    6164
     65    class Meta:
     66        abstract = True
     67
     68    def __unicode__(self):
     69        return self.name
     70
     71class DeferredRelatedA(DeferredRelatedBase):
     72    a_field = models.CharField(max_length=10)
     73
     74class DeferredRelatedB(DeferredRelatedBase):
     75    b_field = models.CharField(max_length=10)
     76
     77class DeferredRelatedC(DeferredRelatedBase):
     78    a = models.ForeignKey(DeferredRelatedA)
     79    b = models.ForeignKey(DeferredRelatedB)
     80    is_published = models.BooleanField()
     81
    6282__test__ = {'API_TESTS': """
    6383(NOTE: Part of the regression test here is merely parsing the model
    6484declaration. The verbose_name, in particular, did not always work.)
  • docs/topics/forms/modelforms.txt

     
    326326    for the missing, but required fields, or use ``save(commit=False)`` and
    327327    manually set any extra required fields::
    328328
    329         instance = Instance(required_field='value')
    330         form = InstanceForm(request.POST, instance=instance)
    331         new_instance = form.save()
     329        class PartialAuthorForm(ModelForm):
     330            class Meta:
     331                model = Author
     332                fields = ('name',)
    332333
    333         instance = form.save(commit=False)
    334         instance.required_field = 'new value'
    335         new_instance = instance.save()
     334        author = Author(title='MR')
     335        form = PartialAuthorForm(request.POST, instance=author)
     336        new_author = form.save()
    336337
     338        # or:
     339
     340        author = form.save(commit=False)
     341        author.title = 'MR'
     342        new_author = author.save()
     343
    337344    See the `section on saving forms`_ for more details on using
    338345    ``save(commit=False)``.
    339346
Back to Top