Ticket #16173: unittest.diff
File unittest.diff, 2.6 KB (added by , 13 years ago) |
---|
-
tests/modeltests/many_to_one/tests.py
4 4 from django.test import TestCase 5 5 from django.core.exceptions import FieldError 6 6 7 from models import Article, Reporter 7 from models import Article, Reporter, ArticleWithDummyManager 8 from models import SuccessfulExecutedException 8 9 9 10 10 11 class ManyToOneTests(TestCase): … … 372 373 # recursive don't cause recursion depth problems under deepcopy. 373 374 self.r.cached_query = Article.objects.filter(reporter=self.r) 374 375 self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>") 376 377 def test_reverse_single_related_manager(self): 378 # This unit test prooves if the dummy managers get methode will be 379 # executed. Related to the ticket #16173. 380 r1 = Reporter.objects.create(first_name='John', last_name='Doh', email='john@doh.com') 381 a1 = ArticleWithDummyManager.objects.create(id=None, headline="Article with dummy manager", 382 pub_date=datetime(2011, 6, 9), reporter_id=r1.id) 383 self.assertRaises(a1.reporter, SuccessfulExecutedException) 384 385 No newline at end of file -
tests/modeltests/many_to_one/models.py
24 24 25 25 class Meta: 26 26 ordering = ('headline',) 27 28 class SuccessfulExecutedException(Exception): 29 pass 30 31 class ArticleDummyManager(models.Manager): 32 use_for_related_fields = True 33 34 def get(self, **kwargs): 35 raise SuccessfulExecutedException 36 37 class ArticleWithDummyManager(Article): 38 default_manager = ArticleDummyManager() -
django/db/models/fields/related.py
311 311 rel_mgr = self.field.rel.to._default_manager 312 312 db = router.db_for_read(self.field.rel.to, instance=instance) 313 313 if getattr(rel_mgr, 'use_for_related_fields', False): 314 rel_obj = rel_mgr. using(db).get(**params)314 rel_obj = rel_mgr.db_manager(db).get(**params) 315 315 else: 316 316 rel_obj = QuerySet(self.field.rel.to).using(db).get(**params) 317 317 setattr(instance, cache_name, rel_obj)