Ticket #10733: deferred_related_fields_test.diff
File deferred_related_fields_test.diff, 4.1 KB (added by , 16 years ago) |
---|
-
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
1 from django.test import TestCase 2 3 class 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
58 58 # object). 59 59 return 'Názov: %s' % self.name 60 60 61 class DeferredRelatedBase(models.Model): 62 name = models.CharField(max_length=10) 63 lots_of_text = models.TextField() 61 64 65 class Meta: 66 abstract = True 67 68 def __unicode__(self): 69 return self.name 70 71 class DeferredRelatedA(DeferredRelatedBase): 72 a_field = models.CharField(max_length=10) 73 74 class DeferredRelatedB(DeferredRelatedBase): 75 b_field = models.CharField(max_length=10) 76 77 class DeferredRelatedC(DeferredRelatedBase): 78 a = models.ForeignKey(DeferredRelatedA) 79 b = models.ForeignKey(DeferredRelatedB) 80 is_published = models.BooleanField() 81 62 82 __test__ = {'API_TESTS': """ 63 83 (NOTE: Part of the regression test here is merely parsing the model 64 84 declaration. The verbose_name, in particular, did not always work.) -
docs/topics/forms/modelforms.txt
326 326 for the missing, but required fields, or use ``save(commit=False)`` and 327 327 manually set any extra required fields:: 328 328 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',) 332 333 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() 336 337 338 # or: 339 340 author = form.save(commit=False) 341 author.title = 'MR' 342 new_author = author.save() 343 337 344 See the `section on saving forms`_ for more details on using 338 345 ``save(commit=False)``. 339 346