Ticket #5535: 5535.1.diff

File 5535.1.diff, 3.5 KB (added by koniiiik, 4 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): 
    10651065
    10661066        try:
    10671067            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)
    10701071        except MultiJoin, e:
    10711072            self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
    10721073                    can_reuse)
  • 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  
    11from datetime import datetime
    22from django.test import TestCase
    3 from django.core.exceptions import FieldError
     3from django.core.exceptions import FieldError, MultipleObjectsReturned
    44from models import Article, Reporter
    55
    66class ManyToOneTests(TestCase):
    class ManyToOneTests(TestCase): 
    226226                "<Article: John's second story>",
    227227                "<Article: This is a test>",
    228228            ])
    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 clause
    232         self.assertRaises(FieldError, Article.objects.filter, reporter_id=self.r.id)
    233229
    234230    def test_reverse_selects(self):
    235231        a3 = Article.objects.create(id=None, headline="Third article",
    class ManyToOneTests(TestCase): 
    370366        self.r.cached_query = Article.objects.filter(reporter=self.r)
    371367        from copy import deepcopy
    372368        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))
Back to Top