2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2010-10-12 02:09:13 +00:00
|
|
|
from operator import attrgetter
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
from .models import Restaurant, Person
|
2010-10-12 02:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModelInheritanceSelectRelatedTests(TestCase):
|
|
|
|
def test_inherited_select_related(self):
|
|
|
|
# Regression test for #7246
|
|
|
|
r1 = Restaurant.objects.create(
|
|
|
|
name="Nobu", serves_sushi=True, serves_steak=False
|
|
|
|
)
|
|
|
|
r2 = Restaurant.objects.create(
|
|
|
|
name="Craft", serves_sushi=False, serves_steak=True
|
|
|
|
)
|
2013-10-19 12:31:38 +00:00
|
|
|
Person.objects.create(name="John", favorite_restaurant=r1)
|
|
|
|
Person.objects.create(name="Jane", favorite_restaurant=r2)
|
2010-10-12 02:09:13 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.order_by("name").select_related(), [
|
|
|
|
"Jane",
|
|
|
|
"John",
|
|
|
|
],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
|
|
|
jane = Person.objects.order_by("name").select_related("favorite_restaurant")[0]
|
|
|
|
self.assertEqual(jane.favorite_restaurant.name, "Craft")
|