Ticket #5535: 5535.2.diff
File 5535.2.diff, 4.3 KB (added by , 14 years ago) |
---|
-
django/db/models/sql/query.py
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 9a5666b..02af89a 100644
a b class Query(object): 1065 1065 1066 1066 try: 1067 1067 field, target, opts, join_list, last, extra_filters = self.setup_joins( 1068 parts, opts, alias, True, allow_many, can_reuse=can_reuse, 1069 negate=negate, process_extras=process_extras) 1068 parts, opts, alias, True, allow_many, allow_explicit_fk=True, 1069 can_reuse=can_reuse, negate=negate, 1070 process_extras=process_extras) 1070 1071 except MultiJoin, e: 1071 1072 self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]), 1072 1073 can_reuse) -
docs/topics/db/queries.txt
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt index 3457913..b63ff10 100644
a b translates (roughly) into the following SQL:: 365 365 366 366 .. _`Keyword Arguments`: http://docs.python.org/tutorial/controlflow.html#keyword-arguments 367 367 368 .. versionchanged:: 1.4 369 370 The field specified in a lookup has to be the name of a model field. 371 There's one exception though, in case of a 372 :class:`~django.db.models.fields.ForeignKey` you can specify the field 373 name suffixed with ``_id``. In this case, the value parameter is expected 374 to contain the raw value of the foreign model's primary key. For 375 example:: 376 377 >>> Entry.objects.filter(blog_id__exact=4) 378 368 379 If you pass an invalid keyword argument, a lookup function will raise 369 380 ``TypeError``. 370 381 -
tests/modeltests/many_to_one/tests.py
diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py index 2d474c0..b9ba711 100644
a b 1 1 from datetime import datetime 2 2 from django.test import TestCase 3 from django.core.exceptions import FieldError 3 from django.core.exceptions import FieldError, MultipleObjectsReturned 4 4 from models import Article, Reporter 5 5 6 6 class ManyToOneTests(TestCase): … … class ManyToOneTests(TestCase): 226 226 "<Article: John's second story>", 227 227 "<Article: This is a test>", 228 228 ]) 229 # You need two underscores between "reporter" and "id" -- not one.230 self.assertRaises(FieldError, Article.objects.filter, reporter_id__exact=self.r.id)231 # You need to specify a comparison clause232 self.assertRaises(FieldError, Article.objects.filter, reporter_id=self.r.id)233 229 234 230 def test_reverse_selects(self): 235 231 a3 = Article.objects.create(id=None, headline="Third article", … … class ManyToOneTests(TestCase): 370 366 self.r.cached_query = Article.objects.filter(reporter=self.r) 371 367 from copy import deepcopy 372 368 self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>") 369 370 def test_explicit_fk(self): 371 # Create a new Article with get_or_create using an explicit value 372 # for a ForeignKey. 373 a2, created = Article.objects.get_or_create(id=None, 374 headline="John's second test", 375 pub_date=datetime(2011, 5, 7), 376 reporter_id=self.r.id) 377 self.assertTrue(created) 378 self.assertEqual(a2.reporter.id, self.r.id) 379 380 # You can specify filters containing the explicit FK value. 381 self.assertQuerysetEqual(Article.objects.filter(reporter_id__exact=self.r.id), 382 [ 383 "<Article: John's second test>", 384 "<Article: This is a test>", 385 ]) 386 387 # Create an Article by Paul for the same date. 388 a3 = Article.objects.create(id=None, headline="Paul's commentary", 389 pub_date=datetime(2011, 5, 7), 390 reporter_id=self.r2.id) 391 self.assertEqual(a3.reporter.id, self.r2.id) 392 393 # Get should respect explicit foreign keys as well. 394 self.assertRaises(MultipleObjectsReturned, 395 Article.objects.get, reporter_id=self.r.id) 396 self.assertEqual(repr(Article.objects.get(reporter_id=self.r2.id, 397 pub_date=datetime(2011, 5, 7))), 398 repr(a3))