mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Further consolidated the model_inheritance tests.
This commit is contained in:
parent
3f76339355
commit
3f01e82c59
@ -73,9 +73,14 @@ class ParkingLot4B(Place, ParkingLot4):
|
||||
pass
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Supplier(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
restaurant = models.ForeignKey(Restaurant)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Wholesaler(Supplier):
|
||||
retailer = models.ForeignKey(Supplier, related_name='wholesale_supplier')
|
||||
|
@ -466,3 +466,25 @@ class ModelInheritanceTest(TestCase):
|
||||
serves_pizza=True, serves_hot_dogs=True)
|
||||
p = Place.objects.all().select_related('restaurant')[0]
|
||||
self.assertIsInstance(p.restaurant.serves_pizza, bool)
|
||||
|
||||
def test_inheritance_select_related(self):
|
||||
# Regression test for #7246
|
||||
r1 = Restaurant.objects.create(
|
||||
name="Nobu", serves_hot_dogs=True, serves_pizza=False
|
||||
)
|
||||
r2 = Restaurant.objects.create(
|
||||
name="Craft", serves_hot_dogs=False, serves_pizza=True
|
||||
)
|
||||
Supplier.objects.create(name="John", restaurant=r1)
|
||||
Supplier.objects.create(name="Jane", restaurant=r2)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Supplier.objects.order_by("name").select_related(), [
|
||||
"Jane",
|
||||
"John",
|
||||
],
|
||||
attrgetter("name")
|
||||
)
|
||||
|
||||
jane = Supplier.objects.order_by("name").select_related("restaurant")[0]
|
||||
self.assertEqual(jane.restaurant.name, "Craft")
|
||||
|
@ -1,37 +0,0 @@
|
||||
"""
|
||||
Regression tests for the interaction between model inheritance and
|
||||
select_related().
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __str__(self):
|
||||
return "%s the place" % self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Restaurant(Place):
|
||||
serves_sushi = models.BooleanField(default=False)
|
||||
serves_steak = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return "%s the restaurant" % self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
favorite_restaurant = models.ForeignKey(Restaurant)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
@ -1,31 +0,0 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from operator import attrgetter
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import Restaurant, Person
|
||||
|
||||
|
||||
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
|
||||
)
|
||||
Person.objects.create(name="John", favorite_restaurant=r1)
|
||||
Person.objects.create(name="Jane", favorite_restaurant=r2)
|
||||
|
||||
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")
|
Loading…
Reference in New Issue
Block a user