mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -9,12 +9,34 @@ from django import forms
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import (
|
||||
ArticleWithAuthor, BachelorParty, BirthdayParty, BusStation, Child,
|
||||
Congressman, DerivedM, InternalCertificationAudit, ItalianRestaurant,
|
||||
M2MChild, MessyBachelorParty, ParkingLot, ParkingLot3, ParkingLot4A,
|
||||
ParkingLot4B, Person, Place, Politician, Profile, QualityControl,
|
||||
Restaurant, SelfRefChild, SelfRefParent, Senator, Supplier, TrainStation,
|
||||
User, Wholesaler,
|
||||
ArticleWithAuthor,
|
||||
BachelorParty,
|
||||
BirthdayParty,
|
||||
BusStation,
|
||||
Child,
|
||||
Congressman,
|
||||
DerivedM,
|
||||
InternalCertificationAudit,
|
||||
ItalianRestaurant,
|
||||
M2MChild,
|
||||
MessyBachelorParty,
|
||||
ParkingLot,
|
||||
ParkingLot3,
|
||||
ParkingLot4A,
|
||||
ParkingLot4B,
|
||||
Person,
|
||||
Place,
|
||||
Politician,
|
||||
Profile,
|
||||
QualityControl,
|
||||
Restaurant,
|
||||
SelfRefChild,
|
||||
SelfRefParent,
|
||||
Senator,
|
||||
Supplier,
|
||||
TrainStation,
|
||||
User,
|
||||
Wholesaler,
|
||||
)
|
||||
|
||||
|
||||
@@ -28,7 +50,7 @@ class ModelInheritanceTest(TestCase):
|
||||
# 'narrow()' API would require a similar approach.
|
||||
|
||||
# Create a child-parent-grandparent chain
|
||||
place1 = Place(name="Guido's House of Pasta", address='944 W. Fullerton')
|
||||
place1 = Place(name="Guido's House of Pasta", address="944 W. Fullerton")
|
||||
place1.save_base(raw=True)
|
||||
restaurant = Restaurant(
|
||||
place_ptr=place1,
|
||||
@@ -36,11 +58,13 @@ class ModelInheritanceTest(TestCase):
|
||||
serves_pizza=False,
|
||||
)
|
||||
restaurant.save_base(raw=True)
|
||||
italian_restaurant = ItalianRestaurant(restaurant_ptr=restaurant, serves_gnocchi=True)
|
||||
italian_restaurant = ItalianRestaurant(
|
||||
restaurant_ptr=restaurant, serves_gnocchi=True
|
||||
)
|
||||
italian_restaurant.save_base(raw=True)
|
||||
|
||||
# Create a child-parent chain with an explicit parent link
|
||||
place2 = Place(name='Main St', address='111 Main St')
|
||||
place2 = Place(name="Main St", address="111 Main St")
|
||||
place2.save_base(raw=True)
|
||||
park = ParkingLot(parent=place2, capacity=100)
|
||||
park.save_base(raw=True)
|
||||
@@ -49,25 +73,37 @@ class ModelInheritanceTest(TestCase):
|
||||
places = list(Place.objects.all())
|
||||
self.assertEqual(places, [place1, place2])
|
||||
|
||||
dicts = list(Restaurant.objects.values('name', 'serves_hot_dogs'))
|
||||
self.assertEqual(dicts, [{
|
||||
'name': "Guido's House of Pasta",
|
||||
'serves_hot_dogs': True
|
||||
}])
|
||||
dicts = list(Restaurant.objects.values("name", "serves_hot_dogs"))
|
||||
self.assertEqual(
|
||||
dicts, [{"name": "Guido's House of Pasta", "serves_hot_dogs": True}]
|
||||
)
|
||||
|
||||
dicts = list(ItalianRestaurant.objects.values(
|
||||
'name', 'serves_hot_dogs', 'serves_gnocchi'))
|
||||
self.assertEqual(dicts, [{
|
||||
'name': "Guido's House of Pasta",
|
||||
'serves_gnocchi': True,
|
||||
'serves_hot_dogs': True,
|
||||
}])
|
||||
dicts = list(
|
||||
ItalianRestaurant.objects.values(
|
||||
"name", "serves_hot_dogs", "serves_gnocchi"
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
dicts,
|
||||
[
|
||||
{
|
||||
"name": "Guido's House of Pasta",
|
||||
"serves_gnocchi": True,
|
||||
"serves_hot_dogs": True,
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
dicts = list(ParkingLot.objects.values('name', 'capacity'))
|
||||
self.assertEqual(dicts, [{
|
||||
'capacity': 100,
|
||||
'name': 'Main St',
|
||||
}])
|
||||
dicts = list(ParkingLot.objects.values("name", "capacity"))
|
||||
self.assertEqual(
|
||||
dicts,
|
||||
[
|
||||
{
|
||||
"capacity": 100,
|
||||
"name": "Main St",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
# You can also update objects when using a raw save.
|
||||
place1.name = "Guido's All New House of Pasta"
|
||||
@@ -79,7 +115,7 @@ class ModelInheritanceTest(TestCase):
|
||||
italian_restaurant.serves_gnocchi = False
|
||||
italian_restaurant.save_base(raw=True)
|
||||
|
||||
place2.name = 'Derelict lot'
|
||||
place2.name = "Derelict lot"
|
||||
place2.save_base(raw=True)
|
||||
|
||||
park.capacity = 50
|
||||
@@ -88,28 +124,46 @@ class ModelInheritanceTest(TestCase):
|
||||
# No extra parent objects after an update, either.
|
||||
places = list(Place.objects.all())
|
||||
self.assertEqual(places, [place2, place1])
|
||||
self.assertEqual(places[0].name, 'Derelict lot')
|
||||
self.assertEqual(places[0].name, "Derelict lot")
|
||||
self.assertEqual(places[1].name, "Guido's All New House of Pasta")
|
||||
|
||||
dicts = list(Restaurant.objects.values('name', 'serves_hot_dogs'))
|
||||
self.assertEqual(dicts, [{
|
||||
'name': "Guido's All New House of Pasta",
|
||||
'serves_hot_dogs': False,
|
||||
}])
|
||||
dicts = list(Restaurant.objects.values("name", "serves_hot_dogs"))
|
||||
self.assertEqual(
|
||||
dicts,
|
||||
[
|
||||
{
|
||||
"name": "Guido's All New House of Pasta",
|
||||
"serves_hot_dogs": False,
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
dicts = list(ItalianRestaurant.objects.values(
|
||||
'name', 'serves_hot_dogs', 'serves_gnocchi'))
|
||||
self.assertEqual(dicts, [{
|
||||
'name': "Guido's All New House of Pasta",
|
||||
'serves_gnocchi': False,
|
||||
'serves_hot_dogs': False,
|
||||
}])
|
||||
dicts = list(
|
||||
ItalianRestaurant.objects.values(
|
||||
"name", "serves_hot_dogs", "serves_gnocchi"
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
dicts,
|
||||
[
|
||||
{
|
||||
"name": "Guido's All New House of Pasta",
|
||||
"serves_gnocchi": False,
|
||||
"serves_hot_dogs": False,
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
dicts = list(ParkingLot.objects.values('name', 'capacity'))
|
||||
self.assertEqual(dicts, [{
|
||||
'capacity': 50,
|
||||
'name': 'Derelict lot',
|
||||
}])
|
||||
dicts = list(ParkingLot.objects.values("name", "capacity"))
|
||||
self.assertEqual(
|
||||
dicts,
|
||||
[
|
||||
{
|
||||
"capacity": 50,
|
||||
"name": "Derelict lot",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
# If you try to raw_save a parent attribute onto a child object,
|
||||
# the attribute will be ignored.
|
||||
@@ -119,28 +173,36 @@ class ModelInheritanceTest(TestCase):
|
||||
|
||||
# Note that the name has not changed
|
||||
# - name is an attribute of Place, not ItalianRestaurant
|
||||
dicts = list(ItalianRestaurant.objects.values(
|
||||
'name', 'serves_hot_dogs', 'serves_gnocchi'))
|
||||
self.assertEqual(dicts, [{
|
||||
'name': "Guido's All New House of Pasta",
|
||||
'serves_gnocchi': False,
|
||||
'serves_hot_dogs': False,
|
||||
}])
|
||||
dicts = list(
|
||||
ItalianRestaurant.objects.values(
|
||||
"name", "serves_hot_dogs", "serves_gnocchi"
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
dicts,
|
||||
[
|
||||
{
|
||||
"name": "Guido's All New House of Pasta",
|
||||
"serves_gnocchi": False,
|
||||
"serves_hot_dogs": False,
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
def test_issue_7105(self):
|
||||
# Regressions tests for #7105: dates() queries should be able to use
|
||||
# fields from the parent model as easily as the child.
|
||||
Child.objects.create(
|
||||
name='child',
|
||||
created=datetime.datetime(2008, 6, 26, 17, 0, 0))
|
||||
datetimes = list(Child.objects.datetimes('created', 'month'))
|
||||
name="child", created=datetime.datetime(2008, 6, 26, 17, 0, 0)
|
||||
)
|
||||
datetimes = list(Child.objects.datetimes("created", "month"))
|
||||
self.assertEqual(datetimes, [datetime.datetime(2008, 6, 1, 0, 0)])
|
||||
|
||||
def test_issue_7276(self):
|
||||
# Regression test for #7276: calling delete() on a model with
|
||||
# multi-table inheritance should delete the associated rows from any
|
||||
# ancestor tables, as well as any descendent objects.
|
||||
place1 = Place(name="Guido's House of Pasta", address='944 W. Fullerton')
|
||||
place1 = Place(name="Guido's House of Pasta", address="944 W. Fullerton")
|
||||
place1.save_base(raw=True)
|
||||
restaurant = Restaurant(
|
||||
place_ptr=place1,
|
||||
@@ -148,14 +210,16 @@ class ModelInheritanceTest(TestCase):
|
||||
serves_pizza=False,
|
||||
)
|
||||
restaurant.save_base(raw=True)
|
||||
italian_restaurant = ItalianRestaurant(restaurant_ptr=restaurant, serves_gnocchi=True)
|
||||
italian_restaurant = ItalianRestaurant(
|
||||
restaurant_ptr=restaurant, serves_gnocchi=True
|
||||
)
|
||||
italian_restaurant.save_base(raw=True)
|
||||
|
||||
ident = ItalianRestaurant.objects.all()[0].id
|
||||
self.assertEqual(Place.objects.get(pk=ident), place1)
|
||||
Restaurant.objects.create(
|
||||
name='a',
|
||||
address='xx',
|
||||
name="a",
|
||||
address="xx",
|
||||
serves_hot_dogs=True,
|
||||
serves_pizza=False,
|
||||
)
|
||||
@@ -187,7 +251,8 @@ class ModelInheritanceTest(TestCase):
|
||||
# equivalent of what the admin interface has to do for the edit-inline
|
||||
# case.
|
||||
suppliers = Supplier.objects.filter(
|
||||
restaurant=Restaurant(name='xx', address='yy'))
|
||||
restaurant=Restaurant(name="xx", address="yy")
|
||||
)
|
||||
suppliers = list(suppliers)
|
||||
self.assertEqual(suppliers, [])
|
||||
|
||||
@@ -213,19 +278,22 @@ class ModelInheritanceTest(TestCase):
|
||||
get_(next/previous)_by_date should work
|
||||
"""
|
||||
c1 = ArticleWithAuthor(
|
||||
headline='ArticleWithAuthor 1',
|
||||
headline="ArticleWithAuthor 1",
|
||||
author="Person 1",
|
||||
pub_date=datetime.datetime(2005, 8, 1, 3, 0))
|
||||
pub_date=datetime.datetime(2005, 8, 1, 3, 0),
|
||||
)
|
||||
c1.save()
|
||||
c2 = ArticleWithAuthor(
|
||||
headline='ArticleWithAuthor 2',
|
||||
headline="ArticleWithAuthor 2",
|
||||
author="Person 2",
|
||||
pub_date=datetime.datetime(2005, 8, 1, 10, 0))
|
||||
pub_date=datetime.datetime(2005, 8, 1, 10, 0),
|
||||
)
|
||||
c2.save()
|
||||
c3 = ArticleWithAuthor(
|
||||
headline='ArticleWithAuthor 3',
|
||||
headline="ArticleWithAuthor 3",
|
||||
author="Person 3",
|
||||
pub_date=datetime.datetime(2005, 8, 2))
|
||||
pub_date=datetime.datetime(2005, 8, 2),
|
||||
)
|
||||
c3.save()
|
||||
|
||||
self.assertEqual(c1.get_next_by_pub_date(), c2)
|
||||
@@ -251,11 +319,11 @@ class ModelInheritanceTest(TestCase):
|
||||
# check it here). Regression test for #9390. This necessarily pokes at
|
||||
# the SQL string for the query, since the duplicate problems are only
|
||||
# apparent at that late stage.
|
||||
qs = ArticleWithAuthor.objects.order_by('pub_date', 'pk')
|
||||
qs = ArticleWithAuthor.objects.order_by("pub_date", "pk")
|
||||
sql = qs.query.get_compiler(qs.db).as_sql()[0]
|
||||
fragment = sql[sql.find('ORDER BY'):]
|
||||
pos = fragment.find('pub_date')
|
||||
self.assertEqual(fragment.find('pub_date', pos + 1), -1)
|
||||
fragment = sql[sql.find("ORDER BY") :]
|
||||
pos = fragment.find("pub_date")
|
||||
self.assertEqual(fragment.find("pub_date", pos + 1), -1)
|
||||
|
||||
def test_queryset_update_on_parent_model(self):
|
||||
"""
|
||||
@@ -268,19 +336,23 @@ class ModelInheritanceTest(TestCase):
|
||||
headline="Hey there!",
|
||||
pub_date=datetime.datetime(2009, 3, 1, 8, 0, 0),
|
||||
)
|
||||
update = ArticleWithAuthor.objects.filter(author='fred').update(headline='Oh, no!')
|
||||
update = ArticleWithAuthor.objects.filter(author="fred").update(
|
||||
headline="Oh, no!"
|
||||
)
|
||||
self.assertEqual(update, 1)
|
||||
update = ArticleWithAuthor.objects.filter(pk=article.pk).update(headline='Oh, no!')
|
||||
update = ArticleWithAuthor.objects.filter(pk=article.pk).update(
|
||||
headline="Oh, no!"
|
||||
)
|
||||
self.assertEqual(update, 1)
|
||||
|
||||
derivedm1 = DerivedM.objects.create(
|
||||
customPK=44,
|
||||
base_name="b1",
|
||||
derived_name='d1',
|
||||
derived_name="d1",
|
||||
)
|
||||
self.assertEqual(derivedm1.customPK, 44)
|
||||
self.assertEqual(derivedm1.base_name, 'b1')
|
||||
self.assertEqual(derivedm1.derived_name, 'd1')
|
||||
self.assertEqual(derivedm1.base_name, "b1")
|
||||
self.assertEqual(derivedm1.derived_name, "d1")
|
||||
derivedms = list(DerivedM.objects.all())
|
||||
self.assertEqual(derivedms, [derivedm1])
|
||||
|
||||
@@ -296,13 +368,13 @@ class ModelInheritanceTest(TestCase):
|
||||
self.assertEqual(ParkingLot4A._meta.pk.name, "parent")
|
||||
ParkingLot4A.objects.create(
|
||||
name="Parking4A",
|
||||
address='21 Jump Street',
|
||||
address="21 Jump Street",
|
||||
)
|
||||
|
||||
self.assertEqual(ParkingLot4B._meta.pk.name, "parent")
|
||||
ParkingLot4A.objects.create(
|
||||
name="Parking4B",
|
||||
address='21 Jump Street',
|
||||
address="21 Jump Street",
|
||||
)
|
||||
|
||||
def test_all_fields_from_abstract_base_class(self):
|
||||
@@ -316,21 +388,21 @@ class ModelInheritanceTest(TestCase):
|
||||
headline="Problems in Django",
|
||||
pub_date=datetime.datetime.now(),
|
||||
quality=10,
|
||||
assignee='adrian',
|
||||
assignee="adrian",
|
||||
)
|
||||
|
||||
def test_abstract_base_class_m2m_relation_inheritance(self):
|
||||
# many-to-many relations defined on an abstract base class are
|
||||
# correctly inherited (and created) on the child class.
|
||||
p1 = Person.objects.create(name='Alice')
|
||||
p2 = Person.objects.create(name='Bob')
|
||||
p3 = Person.objects.create(name='Carol')
|
||||
p4 = Person.objects.create(name='Dave')
|
||||
p1 = Person.objects.create(name="Alice")
|
||||
p2 = Person.objects.create(name="Bob")
|
||||
p3 = Person.objects.create(name="Carol")
|
||||
p4 = Person.objects.create(name="Dave")
|
||||
|
||||
birthday = BirthdayParty.objects.create(name='Birthday party for Alice')
|
||||
birthday = BirthdayParty.objects.create(name="Birthday party for Alice")
|
||||
birthday.attendees.set([p1, p3])
|
||||
|
||||
bachelor = BachelorParty.objects.create(name='Bachelor party for Bob')
|
||||
bachelor = BachelorParty.objects.create(name="Bachelor party for Bob")
|
||||
bachelor.attendees.set([p2, p4])
|
||||
|
||||
parties = list(p1.birthdayparty_set.all())
|
||||
@@ -344,10 +416,10 @@ class ModelInheritanceTest(TestCase):
|
||||
|
||||
# A subclass of a subclass of an abstract model doesn't get its own
|
||||
# accessor.
|
||||
self.assertFalse(hasattr(p2, 'messybachelorparty_set'))
|
||||
self.assertFalse(hasattr(p2, "messybachelorparty_set"))
|
||||
|
||||
# ... but it does inherit the m2m from its parent
|
||||
messy = MessyBachelorParty.objects.create(name='Bachelor party for Dave')
|
||||
messy = MessyBachelorParty.objects.create(name="Bachelor party for Dave")
|
||||
messy.attendees.set([p4])
|
||||
messy_parent = messy.bachelorparty_ptr
|
||||
|
||||
@@ -355,7 +427,7 @@ class ModelInheritanceTest(TestCase):
|
||||
self.assertEqual(parties, [bachelor, messy_parent])
|
||||
|
||||
def test_abstract_base_class_m2m_relation_inheritance_manager_reused(self):
|
||||
p1 = Person.objects.create(name='Alice')
|
||||
p1 = Person.objects.create(name="Alice")
|
||||
self.assertIs(p1.birthdayparty_set, p1.birthdayparty_set)
|
||||
self.assertIs(p1.bachelorparty_set, p1.bachelorparty_set)
|
||||
|
||||
@@ -368,24 +440,15 @@ class ModelInheritanceTest(TestCase):
|
||||
# from an ABC even when there are one or more intermediate
|
||||
# abstract models in the inheritance chain, for consistency with
|
||||
# verbose_name.
|
||||
self.assertEqual(
|
||||
InternalCertificationAudit._meta.verbose_name_plural,
|
||||
'Audits'
|
||||
)
|
||||
self.assertEqual(InternalCertificationAudit._meta.verbose_name_plural, "Audits")
|
||||
|
||||
def test_inherited_nullable_exclude(self):
|
||||
obj = SelfRefChild.objects.create(child_data=37, parent_data=42)
|
||||
self.assertQuerysetEqual(
|
||||
SelfRefParent.objects.exclude(self_data=72), [
|
||||
obj.pk
|
||||
],
|
||||
attrgetter("pk")
|
||||
SelfRefParent.objects.exclude(self_data=72), [obj.pk], attrgetter("pk")
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
SelfRefChild.objects.exclude(self_data=72), [
|
||||
obj.pk
|
||||
],
|
||||
attrgetter("pk")
|
||||
SelfRefChild.objects.exclude(self_data=72), [obj.pk], attrgetter("pk")
|
||||
)
|
||||
|
||||
def test_concrete_abstract_concrete_pk(self):
|
||||
@@ -396,12 +459,20 @@ class ModelInheritanceTest(TestCase):
|
||||
# when more than one model has a concrete->abstract->concrete
|
||||
# inheritance hierarchy.
|
||||
self.assertEqual(
|
||||
len([field for field in BusStation._meta.local_fields if field.primary_key]),
|
||||
1
|
||||
len(
|
||||
[field for field in BusStation._meta.local_fields if field.primary_key]
|
||||
),
|
||||
1,
|
||||
)
|
||||
self.assertEqual(
|
||||
len([field for field in TrainStation._meta.local_fields if field.primary_key]),
|
||||
1
|
||||
len(
|
||||
[
|
||||
field
|
||||
for field in TrainStation._meta.local_fields
|
||||
if field.primary_key
|
||||
]
|
||||
),
|
||||
1,
|
||||
)
|
||||
self.assertIs(BusStation._meta.pk.model, BusStation)
|
||||
self.assertIs(TrainStation._meta.pk.model, TrainStation)
|
||||
@@ -411,43 +482,50 @@ class ModelInheritanceTest(TestCase):
|
||||
A model which has different primary key for the parent model passes
|
||||
unique field checking correctly (#17615).
|
||||
"""
|
||||
|
||||
class ProfileForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Profile
|
||||
fields = '__all__'
|
||||
fields = "__all__"
|
||||
|
||||
User.objects.create(username="user_only")
|
||||
p = Profile.objects.create(username="user_with_profile")
|
||||
form = ProfileForm({'username': "user_with_profile", 'extra': "hello"}, instance=p)
|
||||
form = ProfileForm(
|
||||
{"username": "user_with_profile", "extra": "hello"}, instance=p
|
||||
)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_inheritance_joins(self):
|
||||
# Test for #17502 - check that filtering through two levels of
|
||||
# inheritance chain doesn't generate extra joins.
|
||||
qs = ItalianRestaurant.objects.all()
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 2)
|
||||
qs = ItalianRestaurant.objects.filter(name='foo')
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 2)
|
||||
self.assertEqual(str(qs.query).count("JOIN"), 2)
|
||||
qs = ItalianRestaurant.objects.filter(name="foo")
|
||||
self.assertEqual(str(qs.query).count("JOIN"), 2)
|
||||
|
||||
@expectedFailure
|
||||
def test_inheritance_values_joins(self):
|
||||
# It would be nice (but not too important) to skip the middle join in
|
||||
# this case. Skipping is possible as nothing from the middle model is
|
||||
# used in the qs and top contains direct pointer to the bottom model.
|
||||
qs = ItalianRestaurant.objects.values_list('serves_gnocchi').filter(name='foo')
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
||||
qs = ItalianRestaurant.objects.values_list("serves_gnocchi").filter(name="foo")
|
||||
self.assertEqual(str(qs.query).count("JOIN"), 1)
|
||||
|
||||
def test_issue_21554(self):
|
||||
senator = Senator.objects.create(name='John Doe', title='X', state='Y')
|
||||
senator = Senator.objects.create(name="John Doe", title="X", state="Y")
|
||||
senator = Senator.objects.get(pk=senator.pk)
|
||||
self.assertEqual(senator.name, 'John Doe')
|
||||
self.assertEqual(senator.title, 'X')
|
||||
self.assertEqual(senator.state, 'Y')
|
||||
self.assertEqual(senator.name, "John Doe")
|
||||
self.assertEqual(senator.title, "X")
|
||||
self.assertEqual(senator.state, "Y")
|
||||
|
||||
def test_inheritance_resolve_columns(self):
|
||||
Restaurant.objects.create(name='Bobs Cafe', address="Somewhere",
|
||||
serves_pizza=True, serves_hot_dogs=True)
|
||||
p = Place.objects.all().select_related('restaurant')[0]
|
||||
Restaurant.objects.create(
|
||||
name="Bobs Cafe",
|
||||
address="Somewhere",
|
||||
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):
|
||||
@@ -462,11 +540,12 @@ class ModelInheritanceTest(TestCase):
|
||||
Supplier.objects.create(name="Jane", restaurant=r2)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Supplier.objects.order_by("name").select_related(), [
|
||||
Supplier.objects.order_by("name").select_related(),
|
||||
[
|
||||
"Jane",
|
||||
"John",
|
||||
],
|
||||
attrgetter("name")
|
||||
attrgetter("name"),
|
||||
)
|
||||
|
||||
jane = Supplier.objects.order_by("name").select_related("restaurant")[0]
|
||||
@@ -476,17 +555,19 @@ class ModelInheritanceTest(TestCase):
|
||||
r = Restaurant.objects.create()
|
||||
s = Supplier.objects.create(restaurant=r)
|
||||
# The mismatch between Restaurant and Place is intentional (#28175).
|
||||
self.assertSequenceEqual(Supplier.objects.filter(restaurant__in=Place.objects.all()), [s])
|
||||
self.assertSequenceEqual(
|
||||
Supplier.objects.filter(restaurant__in=Place.objects.all()), [s]
|
||||
)
|
||||
|
||||
def test_ptr_accessor_assigns_state(self):
|
||||
r = Restaurant.objects.create()
|
||||
self.assertIs(r.place_ptr._state.adding, False)
|
||||
self.assertEqual(r.place_ptr._state.db, 'default')
|
||||
self.assertEqual(r.place_ptr._state.db, "default")
|
||||
|
||||
def test_related_filtering_query_efficiency_ticket_15844(self):
|
||||
r = Restaurant.objects.create(
|
||||
name="Guido's House of Pasta",
|
||||
address='944 W. Fullerton',
|
||||
address="944 W. Fullerton",
|
||||
serves_hot_dogs=True,
|
||||
serves_pizza=False,
|
||||
)
|
||||
@@ -499,7 +580,7 @@ class ModelInheritanceTest(TestCase):
|
||||
def test_queries_on_parent_access(self):
|
||||
italian_restaurant = ItalianRestaurant.objects.create(
|
||||
name="Guido's House of Pasta",
|
||||
address='944 W. Fullerton',
|
||||
address="944 W. Fullerton",
|
||||
serves_hot_dogs=True,
|
||||
serves_pizza=False,
|
||||
serves_gnocchi=True,
|
||||
@@ -514,7 +595,9 @@ class ModelInheritanceTest(TestCase):
|
||||
|
||||
# One query is made when accessing the parent objects when the instance
|
||||
# is deferred.
|
||||
italian_restaurant = ItalianRestaurant.objects.only('serves_gnocchi').get(pk=italian_restaurant.pk)
|
||||
italian_restaurant = ItalianRestaurant.objects.only("serves_gnocchi").get(
|
||||
pk=italian_restaurant.pk
|
||||
)
|
||||
with self.assertNumQueries(1):
|
||||
restaurant = italian_restaurant.restaurant_ptr
|
||||
self.assertEqual(restaurant.place_ptr.restaurant, restaurant)
|
||||
@@ -522,16 +605,18 @@ class ModelInheritanceTest(TestCase):
|
||||
|
||||
# No queries are made when accessing the parent objects when the
|
||||
# instance has deferred a field not present in the parent table.
|
||||
italian_restaurant = ItalianRestaurant.objects.defer('serves_gnocchi').get(pk=italian_restaurant.pk)
|
||||
italian_restaurant = ItalianRestaurant.objects.defer("serves_gnocchi").get(
|
||||
pk=italian_restaurant.pk
|
||||
)
|
||||
with self.assertNumQueries(0):
|
||||
restaurant = italian_restaurant.restaurant_ptr
|
||||
self.assertEqual(restaurant.place_ptr.restaurant, restaurant)
|
||||
self.assertEqual(restaurant.italianrestaurant, italian_restaurant)
|
||||
|
||||
def test_id_field_update_on_ancestor_change(self):
|
||||
place1 = Place.objects.create(name='House of Pasta', address='944 Fullerton')
|
||||
place2 = Place.objects.create(name='House of Pizza', address='954 Fullerton')
|
||||
place3 = Place.objects.create(name='Burger house', address='964 Fullerton')
|
||||
place1 = Place.objects.create(name="House of Pasta", address="944 Fullerton")
|
||||
place2 = Place.objects.create(name="House of Pizza", address="954 Fullerton")
|
||||
place3 = Place.objects.create(name="Burger house", address="964 Fullerton")
|
||||
restaurant1 = Restaurant.objects.create(
|
||||
place_ptr=place1,
|
||||
serves_hot_dogs=True,
|
||||
@@ -566,29 +651,29 @@ class ModelInheritanceTest(TestCase):
|
||||
self.assertIsNone(italian_restaurant.id)
|
||||
|
||||
def test_create_new_instance_with_pk_equals_none(self):
|
||||
p1 = Profile.objects.create(username='john')
|
||||
p1 = Profile.objects.create(username="john")
|
||||
p2 = User.objects.get(pk=p1.user_ptr_id).profile
|
||||
# Create a new profile by setting pk = None.
|
||||
p2.pk = None
|
||||
p2.user_ptr_id = None
|
||||
p2.username = 'bill'
|
||||
p2.username = "bill"
|
||||
p2.save()
|
||||
self.assertEqual(Profile.objects.count(), 2)
|
||||
self.assertEqual(User.objects.get(pk=p1.user_ptr_id).username, 'john')
|
||||
self.assertEqual(User.objects.get(pk=p1.user_ptr_id).username, "john")
|
||||
|
||||
def test_create_new_instance_with_pk_equals_none_multi_inheritance(self):
|
||||
c1 = Congressman.objects.create(state='PA', name='John', title='senator 1')
|
||||
c1 = Congressman.objects.create(state="PA", name="John", title="senator 1")
|
||||
c2 = Person.objects.get(pk=c1.pk).congressman
|
||||
# Create a new congressman by setting pk = None.
|
||||
c2.pk = None
|
||||
c2.id = None
|
||||
c2.politician_ptr_id = None
|
||||
c2.name = 'Bill'
|
||||
c2.title = 'senator 2'
|
||||
c2.name = "Bill"
|
||||
c2.title = "senator 2"
|
||||
c2.save()
|
||||
self.assertEqual(Congressman.objects.count(), 2)
|
||||
self.assertEqual(Person.objects.get(pk=c1.pk).name, 'John')
|
||||
self.assertEqual(Person.objects.get(pk=c1.pk).name, "John")
|
||||
self.assertEqual(
|
||||
Politician.objects.get(pk=c1.politician_ptr_id).title,
|
||||
'senator 1',
|
||||
"senator 1",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user