2013-02-10 23:15:49 +08:00
|
|
|
import datetime
|
2015-01-28 20:35:27 +08:00
|
|
|
from copy import deepcopy
|
2011-06-08 04:51:59 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.exceptions import FieldError, MultipleObjectsReturned
|
2019-08-20 15:54:41 +08:00
|
|
|
from django.db import IntegrityError, models, transaction
|
2017-01-01 02:07:35 +08:00
|
|
|
from django.test import TestCase
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy
|
2011-06-08 04:51:59 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import (
|
2021-02-03 00:58:04 +08:00
|
|
|
Article,
|
|
|
|
Category,
|
|
|
|
Child,
|
|
|
|
ChildNullableParent,
|
|
|
|
ChildStringPrimaryKeyParent,
|
|
|
|
City,
|
|
|
|
Country,
|
|
|
|
District,
|
|
|
|
First,
|
|
|
|
Parent,
|
|
|
|
ParentStringPrimaryKey,
|
|
|
|
Record,
|
|
|
|
Relation,
|
|
|
|
Reporter,
|
|
|
|
School,
|
|
|
|
Student,
|
|
|
|
Third,
|
|
|
|
ToFieldChild,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
|
2011-06-08 04:51:59 +08:00
|
|
|
class ManyToOneTests(TestCase):
|
2018-11-24 10:24:25 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-19 12:58:59 +08:00
|
|
|
# Create a few Reporters.
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.r = Reporter(first_name="John", last_name="Smith", email="john@example.com")
|
|
|
|
cls.r.save()
|
|
|
|
cls.r2 = Reporter(
|
|
|
|
first_name="Paul", last_name="Jones", email="paul@example.com"
|
|
|
|
)
|
|
|
|
cls.r2.save()
|
2010-10-19 12:58:59 +08:00
|
|
|
# Create an Article.
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.a = Article(
|
|
|
|
headline="This is a test",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter=cls.r,
|
|
|
|
)
|
|
|
|
cls.a.save()
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
# Article objects have access to their related Reporter objects.
|
|
|
|
r = self.a.reporter
|
|
|
|
self.assertEqual(r.id, self.r.id)
|
|
|
|
self.assertEqual((r.first_name, self.r.last_name), ("John", "Smith"))
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
# You can also instantiate an Article by passing the Reporter's ID
|
|
|
|
# instead of a Reporter object.
|
2016-02-06 02:53:03 +08:00
|
|
|
a3 = Article(
|
|
|
|
headline="Third article",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter_id=self.r.id,
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
a3.save()
|
|
|
|
self.assertEqual(a3.reporter.id, self.r.id)
|
|
|
|
|
|
|
|
# Similarly, the reporter ID can be a string.
|
2016-02-06 02:53:03 +08:00
|
|
|
a4 = Article(
|
|
|
|
headline="Fourth article",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter_id=str(self.r.id),
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
a4.save()
|
|
|
|
self.assertEqual(repr(a4.reporter), "<Reporter: John Smith>")
|
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
# Create an Article via the Reporter object.
|
2016-02-06 02:53:03 +08:00
|
|
|
new_article = self.r.article_set.create(
|
|
|
|
headline="John's second story", pub_date=datetime.date(2005, 7, 29)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
self.assertEqual(repr(new_article), "<Article: John's second story>")
|
|
|
|
self.assertEqual(new_article.reporter.id, self.r.id)
|
|
|
|
|
|
|
|
# Create a new article, and add it to the article set.
|
2013-02-10 23:15:49 +08:00
|
|
|
new_article2 = Article(
|
|
|
|
headline="Paul's story", pub_date=datetime.date(2006, 1, 17)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-03-15 09:25:33 +08:00
|
|
|
msg = (
|
|
|
|
"<Article: Paul's story> instance isn't saved. Use bulk=False or save the "
|
|
|
|
"object first."
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2015-03-15 09:25:33 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.r.article_set.add(new_article2)
|
|
|
|
|
|
|
|
self.r.article_set.add(new_article2, bulk=False)
|
2010-10-19 12:58:59 +08:00
|
|
|
self.assertEqual(new_article2.reporter.id, self.r.id)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
self.r.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article, new_article2, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
# Add the same article to a different article set - check that it moves.
|
|
|
|
self.r2.article_set.add(new_article2)
|
|
|
|
self.assertEqual(new_article2.reporter.id, self.r2.id)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [new_article2])
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
# Adding an object of the wrong type raises TypeError.
|
2013-09-27 02:16:50 +08:00
|
|
|
with transaction.atomic():
|
2016-12-08 06:42:31 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
TypeError, "'Article' instance expected, got <Reporter:"
|
|
|
|
):
|
2013-09-27 02:16:50 +08:00
|
|
|
self.r.article_set.add(self.r2)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
self.r.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
2015-01-30 02:15:27 +08:00
|
|
|
def test_set(self):
|
2016-02-06 02:53:03 +08:00
|
|
|
new_article = self.r.article_set.create(
|
|
|
|
headline="John's second story", pub_date=datetime.date(2005, 7, 29)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-02-06 02:53:03 +08:00
|
|
|
new_article2 = self.r2.article_set.create(
|
|
|
|
headline="Paul's story", pub_date=datetime.date(2006, 1, 17)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-01-30 02:15:27 +08:00
|
|
|
|
|
|
|
# Assign the article to the reporter.
|
|
|
|
new_article2.reporter = self.r
|
|
|
|
new_article2.save()
|
|
|
|
self.assertEqual(repr(new_article2.reporter), "<Reporter: John Smith>")
|
|
|
|
self.assertEqual(new_article2.reporter.id, self.r.id)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.r.article_set.all(),
|
|
|
|
[new_article, new_article2, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [])
|
2015-01-30 02:15:27 +08:00
|
|
|
|
|
|
|
# Set the article back again.
|
|
|
|
self.r2.article_set.set([new_article, new_article2])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r.article_set.all(), [self.a])
|
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
self.r2.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article, new_article2],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2015-01-30 02:15:27 +08:00
|
|
|
|
|
|
|
# Funny case - because the ForeignKey cannot be null,
|
|
|
|
# existing members of the set must remain.
|
|
|
|
self.r.article_set.set([new_article])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
self.r.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [new_article2])
|
2015-01-30 02:15:27 +08:00
|
|
|
|
2015-10-09 05:17:10 +08:00
|
|
|
def test_reverse_assignment_deprecation(self):
|
|
|
|
msg = (
|
|
|
|
"Direct assignment to the reverse side of a related set is "
|
2016-12-31 22:04:09 +08:00
|
|
|
"prohibited. Use article_set.set() instead."
|
2015-10-09 05:17:10 +08:00
|
|
|
)
|
2016-12-31 22:04:09 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2015-10-09 05:17:10 +08:00
|
|
|
self.r2.article_set = []
|
|
|
|
|
2010-10-19 12:58:59 +08:00
|
|
|
def test_assign(self):
|
2016-02-06 02:53:03 +08:00
|
|
|
new_article = self.r.article_set.create(
|
|
|
|
headline="John's second story", pub_date=datetime.date(2005, 7, 29)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-02-06 02:53:03 +08:00
|
|
|
new_article2 = self.r2.article_set.create(
|
|
|
|
headline="Paul's story", pub_date=datetime.date(2006, 1, 17)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-01-30 02:15:27 +08:00
|
|
|
|
2010-10-19 12:58:59 +08:00
|
|
|
# Assign the article to the reporter directly using the descriptor.
|
|
|
|
new_article2.reporter = self.r
|
|
|
|
new_article2.save()
|
|
|
|
self.assertEqual(repr(new_article2.reporter), "<Reporter: John Smith>")
|
|
|
|
self.assertEqual(new_article2.reporter.id, self.r.id)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.r.article_set.all(),
|
|
|
|
[new_article, new_article2, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [])
|
2015-01-30 02:15:27 +08:00
|
|
|
|
2015-10-09 05:17:10 +08:00
|
|
|
# Set the article back again using set() method.
|
|
|
|
self.r2.article_set.set([new_article, new_article2])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r.article_set.all(), [self.a])
|
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
self.r2.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article, new_article2],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
2015-10-09 05:17:10 +08:00
|
|
|
# Because the ForeignKey cannot be null, existing members of the set
|
|
|
|
# must remain.
|
|
|
|
self.r.article_set.set([new_article])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
self.r.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [new_article2])
|
2010-10-19 12:58:59 +08:00
|
|
|
# Reporter cannot be null - there should not be a clear or remove method
|
|
|
|
self.assertFalse(hasattr(self.r2.article_set, "remove"))
|
|
|
|
self.assertFalse(hasattr(self.r2.article_set, "clear"))
|
|
|
|
|
2019-07-26 16:18:24 +08:00
|
|
|
def test_assign_fk_id_value(self):
|
|
|
|
parent = Parent.objects.create(name="jeff")
|
|
|
|
child1 = Child.objects.create(name="frank", parent=parent)
|
|
|
|
child2 = Child.objects.create(name="randy", parent=parent)
|
|
|
|
parent.bestchild = child1
|
|
|
|
parent.save()
|
|
|
|
parent.bestchild_id = child2.pk
|
|
|
|
parent.save()
|
|
|
|
self.assertEqual(parent.bestchild_id, child2.pk)
|
|
|
|
self.assertFalse(Parent.bestchild.is_cached(parent))
|
|
|
|
self.assertEqual(parent.bestchild, child2)
|
|
|
|
self.assertTrue(Parent.bestchild.is_cached(parent))
|
|
|
|
# Reassigning the same value doesn't clear cached instance.
|
|
|
|
parent.bestchild_id = child2.pk
|
|
|
|
self.assertTrue(Parent.bestchild.is_cached(parent))
|
|
|
|
|
2019-07-23 20:04:06 +08:00
|
|
|
def test_assign_fk_id_none(self):
|
|
|
|
parent = Parent.objects.create(name="jeff")
|
|
|
|
child = Child.objects.create(name="frank", parent=parent)
|
|
|
|
parent.bestchild = child
|
|
|
|
parent.save()
|
|
|
|
parent.bestchild_id = None
|
|
|
|
parent.save()
|
|
|
|
self.assertIsNone(parent.bestchild_id)
|
|
|
|
self.assertFalse(Parent.bestchild.is_cached(parent))
|
|
|
|
self.assertIsNone(parent.bestchild)
|
|
|
|
self.assertTrue(Parent.bestchild.is_cached(parent))
|
|
|
|
|
2010-10-19 12:58:59 +08:00
|
|
|
def test_selects(self):
|
2020-10-19 00:29:52 +08:00
|
|
|
new_article1 = self.r.article_set.create(
|
|
|
|
headline="John's second story",
|
|
|
|
pub_date=datetime.date(2005, 7, 29),
|
|
|
|
)
|
|
|
|
new_article2 = self.r2.article_set.create(
|
|
|
|
headline="Paul's story",
|
|
|
|
pub_date=datetime.date(2006, 1, 17),
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# Reporter objects have access to their related Article objects.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.r.article_set.all(),
|
|
|
|
[new_article1, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.r.article_set.filter(headline__startswith="This"), [self.a]
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
self.assertEqual(self.r.article_set.count(), 2)
|
|
|
|
self.assertEqual(self.r2.article_set.count(), 1)
|
|
|
|
# Get articles by id
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Article.objects.filter(id__exact=self.a.id), [self.a])
|
|
|
|
self.assertSequenceEqual(Article.objects.filter(pk=self.a.id), [self.a])
|
2010-10-19 12:58:59 +08:00
|
|
|
# Query on an article property
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Article.objects.filter(headline__startswith="This"), [self.a]
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# The API automatically follows relationships as far as you need.
|
|
|
|
# Use double underscores to separate relationships.
|
|
|
|
# This works as many levels deep as you want. There's no limit.
|
|
|
|
# Find all Articles for any Reporter whose first name is "John".
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
Article.objects.filter(reporter__first_name__exact="John"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2016-10-27 15:53:39 +08:00
|
|
|
# Implied __exact also works
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
Article.objects.filter(reporter__first_name="John"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# Query twice over the related field.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
Article.objects.filter(
|
|
|
|
reporter__first_name__exact="John", reporter__last_name__exact="Smith"
|
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# The underlying query only makes one join when a related table is
|
|
|
|
# referenced twice.
|
2016-02-06 02:53:03 +08:00
|
|
|
queryset = Article.objects.filter(
|
|
|
|
reporter__first_name__exact="John", reporter__last_name__exact="Smith"
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
self.assertNumQueries(1, list, queryset)
|
|
|
|
self.assertEqual(
|
|
|
|
queryset.query.get_compiler(queryset.db).as_sql()[0].count("INNER JOIN"), 1
|
|
|
|
)
|
|
|
|
|
|
|
|
# The automatically joined table has a predictable name.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Article.objects.filter(reporter__first_name__exact="John").extra(
|
|
|
|
where=["many_to_one_reporter.last_name='Smith'"]
|
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2017-01-21 05:04:05 +08:00
|
|
|
# ... and should work fine with the string that comes out of
|
|
|
|
# forms.Form.cleaned_data.
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2013-12-09 01:20:06 +08:00
|
|
|
(
|
|
|
|
Article.objects.filter(reporter__first_name__exact="John").extra(
|
|
|
|
where=["many_to_one_reporter.last_name='%s'" % "Smith"]
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-12-09 01:20:06 +08:00
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# Find all Articles for a Reporter.
|
|
|
|
# Use direct ID check, pk check, and object comparison
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Article.objects.filter(reporter__id__exact=self.r.id),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Article.objects.filter(reporter__pk=self.r.id),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Article.objects.filter(reporter=self.r.id),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Article.objects.filter(reporter=self.r),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2013-10-27 03:15:03 +08:00
|
|
|
Article.objects.filter(reporter__in=[self.r.id, self.r2.id]).distinct(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, new_article2, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2013-10-27 03:15:03 +08:00
|
|
|
Article.objects.filter(reporter__in=[self.r, self.r2]).distinct(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, new_article2, self.a],
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# You can also use a queryset instead of a literal list of instances.
|
|
|
|
# The queryset must be reduced to a list of values using values(),
|
|
|
|
# then converted into a query
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Article.objects.filter(
|
2013-10-20 07:33:10 +08:00
|
|
|
reporter__in=Reporter.objects.filter(first_name="John")
|
|
|
|
.values("pk")
|
|
|
|
.query
|
|
|
|
).distinct(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article1, self.a],
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
def test_reverse_selects(self):
|
2016-02-06 02:53:03 +08:00
|
|
|
a3 = Article.objects.create(
|
|
|
|
headline="Third article",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter_id=self.r.id,
|
|
|
|
)
|
|
|
|
Article.objects.create(
|
|
|
|
headline="Fourth article",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter_id=self.r.id,
|
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
john_smith = [self.r]
|
2010-10-19 12:58:59 +08:00
|
|
|
# Reporters can be queried
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(id__exact=self.r.id), john_smith
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.filter(pk=self.r.id), john_smith)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(first_name__startswith="John"), john_smith
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
# Reporters can query in opposite direction of ForeignKey definition
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__id__exact=self.a.id), john_smith
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__pk=self.a.id), john_smith
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.filter(article=self.a.id), john_smith)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.filter(article=self.a), john_smith)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__in=[self.a.id, a3.id]).distinct(),
|
|
|
|
john_smith,
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__in=[self.a.id, a3]).distinct(), john_smith
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__in=[self.a, a3]).distinct(), john_smith
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertCountEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Reporter.objects.filter(article__headline__startswith="T"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.r, self.r],
|
2012-12-13 19:33:11 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__headline__startswith="T").distinct(),
|
|
|
|
john_smith,
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
# Counting in the opposite direction works in conjunction with distinct()
|
2016-02-06 02:53:03 +08:00
|
|
|
self.assertEqual(
|
|
|
|
Reporter.objects.filter(article__headline__startswith="T").count(), 2
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Reporter.objects.filter(article__headline__startswith="T")
|
|
|
|
.distinct()
|
|
|
|
.count(),
|
|
|
|
1,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
# Queries can go round in circles.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertCountEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Reporter.objects.filter(article__reporter__first_name__startswith="John"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.r, self.r, self.r],
|
2012-12-13 19:33:11 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Reporter.objects.filter(
|
|
|
|
article__reporter__first_name__startswith="John"
|
2013-10-20 07:33:10 +08:00
|
|
|
).distinct(),
|
2020-10-19 00:29:52 +08:00
|
|
|
john_smith,
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:58:59 +08:00
|
|
|
Reporter.objects.filter(article__reporter__exact=self.r).distinct(),
|
2016-02-06 02:53:03 +08:00
|
|
|
john_smith,
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Implied __exact also works.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Reporter.objects.filter(article__reporter=self.r).distinct(), john_smith
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
# It's possible to use values() calls across many-to-one relations.
|
|
|
|
# (Note, too, that we clear the ordering here so as not to drag the
|
|
|
|
# 'headline' field into the columns being used to determine uniqueness)
|
2012-06-08 00:08:47 +08:00
|
|
|
d = {"reporter__first_name": "John", "reporter__last_name": "Smith"}
|
2016-02-06 02:53:03 +08:00
|
|
|
qs = (
|
|
|
|
Article.objects.filter(
|
|
|
|
reporter=self.r,
|
|
|
|
)
|
|
|
|
.distinct()
|
|
|
|
.order_by()
|
|
|
|
.values("reporter__first_name", "reporter__last_name")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-02-06 02:53:03 +08:00
|
|
|
self.assertEqual([d], list(qs))
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
def test_select_related(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# Article.objects.select_related().dates() works properly when there
|
|
|
|
# are multiple Articles with the same date but different foreign-key
|
2010-10-19 12:58:59 +08:00
|
|
|
# objects (Reporters).
|
|
|
|
r1 = Reporter.objects.create(
|
|
|
|
first_name="Mike", last_name="Royko", email="royko@suntimes.com"
|
|
|
|
)
|
|
|
|
r2 = Reporter.objects.create(
|
|
|
|
first_name="John", last_name="Kass", email="jkass@tribune.com"
|
|
|
|
)
|
2013-02-10 23:15:49 +08:00
|
|
|
Article.objects.create(
|
|
|
|
headline="First", pub_date=datetime.date(1980, 4, 23), reporter=r1
|
|
|
|
)
|
|
|
|
Article.objects.create(
|
|
|
|
headline="Second", pub_date=datetime.date(1980, 4, 23), reporter=r2
|
|
|
|
)
|
2016-02-06 02:53:03 +08:00
|
|
|
self.assertEqual(
|
|
|
|
list(Article.objects.select_related().dates("pub_date", "day")),
|
|
|
|
[datetime.date(1980, 4, 23), datetime.date(2005, 7, 27)],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
list(Article.objects.select_related().dates("pub_date", "month")),
|
|
|
|
[datetime.date(1980, 4, 1), datetime.date(2005, 7, 1)],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
list(Article.objects.select_related().dates("pub_date", "year")),
|
|
|
|
[datetime.date(1980, 1, 1), datetime.date(2005, 1, 1)],
|
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
|
|
|
|
def test_delete(self):
|
2020-10-19 00:29:52 +08:00
|
|
|
new_article1 = self.r.article_set.create(
|
|
|
|
headline="John's second story",
|
|
|
|
pub_date=datetime.date(2005, 7, 29),
|
|
|
|
)
|
|
|
|
new_article2 = self.r2.article_set.create(
|
|
|
|
headline="Paul's story",
|
|
|
|
pub_date=datetime.date(2006, 1, 17),
|
|
|
|
)
|
|
|
|
new_article3 = Article.objects.create(
|
|
|
|
headline="Third article",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter_id=self.r.id,
|
|
|
|
)
|
|
|
|
new_article4 = Article.objects.create(
|
2016-02-06 02:53:03 +08:00
|
|
|
headline="Fourth article",
|
|
|
|
pub_date=datetime.date(2005, 7, 27),
|
|
|
|
reporter_id=str(self.r.id),
|
|
|
|
)
|
2020-11-14 05:26:30 +08:00
|
|
|
# If you delete a reporter, their articles will be deleted.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
Article.objects.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article4, new_article1, new_article2, new_article3, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
Reporter.objects.order_by("first_name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.r, self.r2],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2010-10-19 12:58:59 +08:00
|
|
|
self.r2.delete()
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-02-06 02:53:03 +08:00
|
|
|
Article.objects.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[new_article4, new_article1, new_article3, self.a],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.order_by("first_name"), [self.r])
|
2010-10-19 12:58:59 +08:00
|
|
|
# You can delete using a JOIN in the query.
|
|
|
|
Reporter.objects.filter(article__headline__startswith="This").delete()
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
|
|
|
self.assertSequenceEqual(Article.objects.all(), [])
|
2010-10-19 12:58:59 +08:00
|
|
|
|
2011-06-28 18:16:18 +08:00
|
|
|
def test_explicit_fk(self):
|
|
|
|
# Create a new Article with get_or_create using an explicit value
|
|
|
|
# for a ForeignKey.
|
2016-02-06 02:53:03 +08:00
|
|
|
a2, created = Article.objects.get_or_create(
|
|
|
|
headline="John's second test",
|
|
|
|
pub_date=datetime.date(2011, 5, 7),
|
|
|
|
reporter_id=self.r.id,
|
|
|
|
)
|
2011-06-28 18:16:18 +08:00
|
|
|
self.assertTrue(created)
|
|
|
|
self.assertEqual(a2.reporter.id, self.r.id)
|
|
|
|
|
|
|
|
# You can specify filters containing the explicit FK value.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2011-06-28 18:16:18 +08:00
|
|
|
Article.objects.filter(reporter_id__exact=self.r.id),
|
2020-10-19 00:29:52 +08:00
|
|
|
[a2, self.a],
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2011-06-28 18:16:18 +08:00
|
|
|
|
|
|
|
# Create an Article by Paul for the same date.
|
2016-02-06 02:53:03 +08:00
|
|
|
a3 = Article.objects.create(
|
|
|
|
headline="Paul's commentary",
|
|
|
|
pub_date=datetime.date(2011, 5, 7),
|
|
|
|
reporter_id=self.r2.id,
|
|
|
|
)
|
2011-06-28 18:16:18 +08:00
|
|
|
self.assertEqual(a3.reporter.id, self.r2.id)
|
|
|
|
|
|
|
|
# Get should respect explicit foreign keys as well.
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "get() returned more than one Article -- it returned 2!"
|
|
|
|
with self.assertRaisesMessage(MultipleObjectsReturned, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
Article.objects.get(reporter_id=self.r.id)
|
2016-02-06 02:53:03 +08:00
|
|
|
self.assertEqual(
|
|
|
|
repr(a3),
|
|
|
|
repr(
|
|
|
|
Article.objects.get(
|
|
|
|
reporter_id=self.r2.id, pub_date=datetime.date(2011, 5, 7)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
|
|
|
),
|
2016-02-06 02:53:03 +08:00
|
|
|
)
|
2011-09-30 18:41:25 +08:00
|
|
|
|
2014-09-24 00:29:17 +08:00
|
|
|
def test_deepcopy_and_circular_references(self):
|
|
|
|
# Regression for #12876 -- Model methods that include queries that
|
|
|
|
# recursive don't cause recursion depth problems under deepcopy.
|
|
|
|
self.r.cached_query = Article.objects.filter(reporter=self.r)
|
|
|
|
self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>")
|
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
def test_manager_class_caching(self):
|
|
|
|
r1 = Reporter.objects.create(first_name="Mike")
|
|
|
|
r2 = Reporter.objects.create(first_name="John")
|
|
|
|
|
|
|
|
# Same twice
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(r1.article_set.__class__, r1.article_set.__class__)
|
2011-09-30 18:41:25 +08:00
|
|
|
|
|
|
|
# Same as each other
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(r1.article_set.__class__, r2.article_set.__class__)
|
2012-03-04 03:02:49 +08:00
|
|
|
|
2017-01-27 03:58:33 +08:00
|
|
|
def test_create_relation_with_gettext_lazy(self):
|
2016-02-06 02:53:03 +08:00
|
|
|
reporter = Reporter.objects.create(
|
|
|
|
first_name="John", last_name="Smith", email="john.smith@example.com"
|
|
|
|
)
|
2017-01-27 03:58:33 +08:00
|
|
|
lazy = gettext_lazy("test")
|
2016-02-06 02:53:03 +08:00
|
|
|
reporter.article_set.create(headline=lazy, pub_date=datetime.date(2011, 6, 10))
|
2016-12-08 06:42:31 +08:00
|
|
|
notlazy = str(lazy)
|
2012-03-04 03:02:49 +08:00
|
|
|
article = reporter.article_set.get()
|
|
|
|
self.assertEqual(article.headline, notlazy)
|
2012-07-17 17:24:56 +08:00
|
|
|
|
|
|
|
def test_values_list_exception(self):
|
|
|
|
expected_message = (
|
|
|
|
"Cannot resolve keyword 'notafield' into field. Choices are: %s"
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-01-04 16:50:08 +08:00
|
|
|
reporter_fields = ", ".join(sorted(f.name for f in Reporter._meta.get_fields()))
|
|
|
|
with self.assertRaisesMessage(FieldError, expected_message % reporter_fields):
|
|
|
|
Article.objects.values_list("reporter__notafield")
|
|
|
|
article_fields = ", ".join(
|
|
|
|
["EXTRA"] + sorted(f.name for f in Article._meta.get_fields())
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(FieldError, expected_message % article_fields):
|
|
|
|
Article.objects.extra(select={"EXTRA": "EXTRA_SELECT"}).values_list(
|
|
|
|
"notafield"
|
|
|
|
)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
def test_fk_assignment_and_related_object_cache(self):
|
|
|
|
# Tests of ForeignKey assignment and the related-object cache (see #6886).
|
|
|
|
|
|
|
|
p = Parent.objects.create(name="Parent")
|
|
|
|
c = Child.objects.create(name="Child", parent=p)
|
|
|
|
|
|
|
|
# Look up the object again so that we get a "fresh" object.
|
|
|
|
c = Child.objects.get(name="Child")
|
|
|
|
p = c.parent
|
|
|
|
|
|
|
|
# Accessing the related object again returns the exactly same object.
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(c.parent, p)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# But if we kill the cache, we get a new object.
|
2016-06-05 07:10:37 +08:00
|
|
|
del c._state.fields_cache["parent"]
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNot(c.parent, p)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# Assigning a new object results in that object getting cached immediately.
|
|
|
|
p2 = Parent.objects.create(name="Parent 2")
|
|
|
|
c.parent = p2
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(c.parent, p2)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# Assigning None succeeds if field is null=True.
|
|
|
|
p.bestchild = None
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(p.bestchild)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# bestchild should still be None after saving.
|
|
|
|
p.save()
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(p.bestchild)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# bestchild should still be None after fetching the object again.
|
|
|
|
p = Parent.objects.get(name="Parent")
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(p.bestchild)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
2016-02-11 09:46:57 +08:00
|
|
|
# Assigning None will not fail: Child.parent is null=False.
|
|
|
|
setattr(c, "parent", None)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# You also can't assign an object of the wrong type here
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
'Cannot assign "<First: First object (1)>": "Child.parent" must '
|
|
|
|
'be a "Parent" instance.'
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
setattr(c, "parent", First(id=1, second=1))
|
2014-09-24 00:29:17 +08:00
|
|
|
|
2016-02-11 09:46:57 +08:00
|
|
|
# You can assign None to Child.parent during object creation.
|
|
|
|
Child(name="xyzzy", parent=None)
|
|
|
|
|
|
|
|
# But when trying to save a Child with parent=None, the database will
|
|
|
|
# raise IntegrityError.
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
2016-01-17 19:26:39 +08:00
|
|
|
Child.objects.create(name="xyzzy", parent=None)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# Creation using keyword argument should cache the related object.
|
|
|
|
p = Parent.objects.get(name="Parent")
|
|
|
|
c = Child(parent=p)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(c.parent, p)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# Creation using keyword argument and unsaved related instance (#8070).
|
|
|
|
p = Parent()
|
2015-07-24 19:51:40 +08:00
|
|
|
msg = (
|
|
|
|
"save() prohibited to prevent data loss due to unsaved related object "
|
|
|
|
"'parent'."
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2015-07-24 19:51:40 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
Child.objects.create(parent=p)
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
ToFieldChild.objects.create(parent=p)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
# Creation using attname keyword argument and an id will cause the
|
|
|
|
# related object to be fetched.
|
|
|
|
p = Parent.objects.get(name="Parent")
|
|
|
|
c = Child(parent_id=p.id)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNot(c.parent, p)
|
2014-09-24 00:29:17 +08:00
|
|
|
self.assertEqual(c.parent, p)
|
|
|
|
|
2022-05-27 20:13:12 +08:00
|
|
|
def test_save_parent_after_assign(self):
|
|
|
|
category = Category(name="cats")
|
|
|
|
record = Record(category=category)
|
|
|
|
category.save()
|
|
|
|
record.save()
|
|
|
|
category.name = "dogs"
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertEqual(category.id, record.category_id)
|
|
|
|
self.assertEqual(category.name, record.category.name)
|
|
|
|
|
2019-05-20 20:06:30 +08:00
|
|
|
def test_save_nullable_fk_after_parent(self):
|
|
|
|
parent = Parent()
|
|
|
|
child = ChildNullableParent(parent=parent)
|
|
|
|
parent.save()
|
|
|
|
child.save()
|
|
|
|
child.refresh_from_db()
|
|
|
|
self.assertEqual(child.parent, parent)
|
|
|
|
|
2019-05-20 20:04:26 +08:00
|
|
|
def test_save_nullable_fk_after_parent_with_to_field(self):
|
|
|
|
parent = Parent(name="jeff")
|
|
|
|
child = ToFieldChild(parent=parent)
|
|
|
|
parent.save()
|
|
|
|
child.save()
|
|
|
|
child.refresh_from_db()
|
2021-02-03 00:58:04 +08:00
|
|
|
self.assertEqual(child.parent, parent)
|
|
|
|
self.assertEqual(child.parent_id, parent.name)
|
|
|
|
|
|
|
|
def test_save_fk_after_parent_with_non_numeric_pk_set_on_child(self):
|
|
|
|
parent = ParentStringPrimaryKey()
|
|
|
|
child = ChildStringPrimaryKeyParent(parent=parent)
|
|
|
|
child.parent.name = "jeff"
|
|
|
|
parent.save()
|
|
|
|
child.save()
|
|
|
|
child.refresh_from_db()
|
2019-05-20 20:04:26 +08:00
|
|
|
self.assertEqual(child.parent, parent)
|
|
|
|
self.assertEqual(child.parent_id, parent.name)
|
|
|
|
|
2015-07-02 16:43:15 +08:00
|
|
|
def test_fk_to_bigautofield(self):
|
|
|
|
ch = City.objects.create(name="Chicago")
|
|
|
|
District.objects.create(city=ch, name="Far South")
|
|
|
|
District.objects.create(city=ch, name="North")
|
|
|
|
|
|
|
|
ny = City.objects.create(name="New York", id=2**33)
|
|
|
|
District.objects.create(city=ny, name="Brooklyn")
|
|
|
|
District.objects.create(city=ny, name="Manhattan")
|
|
|
|
|
2019-07-27 05:05:22 +08:00
|
|
|
def test_fk_to_smallautofield(self):
|
|
|
|
us = Country.objects.create(name="United States")
|
|
|
|
City.objects.create(country=us, name="Chicago")
|
|
|
|
City.objects.create(country=us, name="New York")
|
|
|
|
|
|
|
|
uk = Country.objects.create(name="United Kingdom", id=2**11)
|
|
|
|
City.objects.create(country=uk, name="London")
|
|
|
|
City.objects.create(country=uk, name="Edinburgh")
|
|
|
|
|
2014-09-24 00:29:17 +08:00
|
|
|
def test_multiple_foreignkeys(self):
|
|
|
|
# Test of multiple ForeignKeys to the same model (bug #7125).
|
|
|
|
c1 = Category.objects.create(name="First")
|
|
|
|
c2 = Category.objects.create(name="Second")
|
|
|
|
c3 = Category.objects.create(name="Third")
|
|
|
|
r1 = Record.objects.create(category=c1)
|
|
|
|
r2 = Record.objects.create(category=c1)
|
|
|
|
r3 = Record.objects.create(category=c2)
|
|
|
|
r4 = Record.objects.create(category=c2)
|
|
|
|
r5 = Record.objects.create(category=c3)
|
|
|
|
Relation.objects.create(left=r1, right=r2)
|
|
|
|
Relation.objects.create(left=r3, right=r4)
|
2020-10-19 00:29:52 +08:00
|
|
|
rel = Relation.objects.create(left=r1, right=r3)
|
2014-09-24 00:29:17 +08:00
|
|
|
Relation.objects.create(left=r5, right=r2)
|
|
|
|
Relation.objects.create(left=r3, right=r2)
|
|
|
|
|
|
|
|
q1 = Relation.objects.filter(
|
|
|
|
left__category__name__in=["First"], right__category__name__in=["Second"]
|
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(q1, [rel])
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
q2 = Category.objects.filter(
|
|
|
|
record__left_set__right__category__name="Second"
|
|
|
|
).order_by("name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(q2, [c1, c2])
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
p = Parent.objects.create(name="Parent")
|
|
|
|
c = Child.objects.create(name="Child", parent=p)
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = 'Cannot assign "%r": "Child.parent" must be a "Parent" instance.' % c
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
Child.objects.create(name="Grandchild", parent=c)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
def test_fk_instantiation_outside_model(self):
|
|
|
|
# Regression for #12190 -- Should be able to instantiate a FK outside
|
|
|
|
# of a model, and interrogate its related field.
|
2015-07-22 22:43:21 +08:00
|
|
|
cat = models.ForeignKey(Category, models.CASCADE)
|
2015-02-26 22:19:17 +08:00
|
|
|
self.assertEqual("id", cat.remote_field.get_related_field().name)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
def test_relation_unsaved(self):
|
|
|
|
Third.objects.create(name="Third 1")
|
|
|
|
Third.objects.create(name="Third 2")
|
|
|
|
th = Third(name="testing")
|
2022-01-14 20:44:17 +08:00
|
|
|
# The object isn't saved and the relation cannot be used.
|
|
|
|
msg = (
|
|
|
|
"'Third' instance needs to have a primary key value before this "
|
|
|
|
"relationship can be used."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
th.child_set.count()
|
2022-08-25 03:14:32 +08:00
|
|
|
# The reverse foreign key manager can be created.
|
|
|
|
self.assertEqual(th.child_set.model, Third)
|
|
|
|
|
2014-09-24 00:29:17 +08:00
|
|
|
th.save()
|
2017-11-07 11:11:39 +08:00
|
|
|
# Now the model is saved, so we will need to execute a query.
|
2014-09-24 00:29:17 +08:00
|
|
|
with self.assertNumQueries(1):
|
|
|
|
self.assertEqual(th.child_set.count(), 0)
|
|
|
|
|
|
|
|
def test_related_object(self):
|
|
|
|
public_school = School.objects.create(is_public=True)
|
|
|
|
public_student = Student.objects.create(school=public_school)
|
|
|
|
|
|
|
|
private_school = School.objects.create(is_public=False)
|
|
|
|
private_student = Student.objects.create(school=private_school)
|
|
|
|
|
|
|
|
# Only one school is available via all() due to the custom default manager.
|
2017-04-10 20:59:29 +08:00
|
|
|
self.assertSequenceEqual(School.objects.all(), [public_school])
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
self.assertEqual(public_student.school, public_school)
|
|
|
|
|
2017-11-07 11:11:39 +08:00
|
|
|
# Make sure the base manager is used so that a student can still access
|
2014-09-24 00:29:17 +08:00
|
|
|
# its related school even if the default manager doesn't normally
|
|
|
|
# allow it.
|
|
|
|
self.assertEqual(private_student.school, private_school)
|
|
|
|
|
2016-04-17 19:55:55 +08:00
|
|
|
School._meta.base_manager_name = "objects"
|
|
|
|
School._meta._expire_cache()
|
|
|
|
try:
|
|
|
|
private_student = Student.objects.get(pk=private_student.pk)
|
|
|
|
with self.assertRaises(School.DoesNotExist):
|
|
|
|
private_student.school
|
|
|
|
finally:
|
|
|
|
School._meta.base_manager_name = None
|
|
|
|
School._meta._expire_cache()
|
|
|
|
|
2014-09-24 00:29:17 +08:00
|
|
|
def test_hasattr_related_object(self):
|
|
|
|
# The exception raised on attribute access when a related object
|
|
|
|
# doesn't exist should be an instance of a subclass of `AttributeError`
|
|
|
|
# refs #21563
|
|
|
|
self.assertFalse(hasattr(Article(), "reporter"))
|
2016-06-05 20:15:00 +08:00
|
|
|
|
|
|
|
def test_clear_after_prefetch(self):
|
|
|
|
c = City.objects.create(name="Musical City")
|
2020-10-19 00:29:52 +08:00
|
|
|
d = District.objects.create(name="Ladida", city=c)
|
2016-06-05 20:15:00 +08:00
|
|
|
city = City.objects.prefetch_related("districts").get(id=c.id)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(city.districts.all(), [d])
|
2016-06-05 20:15:00 +08:00
|
|
|
city.districts.clear()
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(city.districts.all(), [])
|
2016-06-05 20:15:00 +08:00
|
|
|
|
|
|
|
def test_remove_after_prefetch(self):
|
|
|
|
c = City.objects.create(name="Musical City")
|
|
|
|
d = District.objects.create(name="Ladida", city=c)
|
|
|
|
city = City.objects.prefetch_related("districts").get(id=c.id)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(city.districts.all(), [d])
|
2016-06-05 20:15:00 +08:00
|
|
|
city.districts.remove(d)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(city.districts.all(), [])
|
2016-06-05 20:15:00 +08:00
|
|
|
|
|
|
|
def test_add_after_prefetch(self):
|
|
|
|
c = City.objects.create(name="Musical City")
|
|
|
|
District.objects.create(name="Ladida", city=c)
|
|
|
|
d2 = District.objects.create(name="Ladidu")
|
|
|
|
city = City.objects.prefetch_related("districts").get(id=c.id)
|
|
|
|
self.assertEqual(city.districts.count(), 1)
|
|
|
|
city.districts.add(d2)
|
|
|
|
self.assertEqual(city.districts.count(), 2)
|
|
|
|
|
|
|
|
def test_set_after_prefetch(self):
|
|
|
|
c = City.objects.create(name="Musical City")
|
|
|
|
District.objects.create(name="Ladida", city=c)
|
|
|
|
d2 = District.objects.create(name="Ladidu")
|
|
|
|
city = City.objects.prefetch_related("districts").get(id=c.id)
|
|
|
|
self.assertEqual(city.districts.count(), 1)
|
|
|
|
city.districts.set([d2])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(city.districts.all(), [d2])
|
2016-06-05 20:15:00 +08:00
|
|
|
|
|
|
|
def test_add_then_remove_after_prefetch(self):
|
|
|
|
c = City.objects.create(name="Musical City")
|
|
|
|
District.objects.create(name="Ladida", city=c)
|
|
|
|
d2 = District.objects.create(name="Ladidu")
|
|
|
|
city = City.objects.prefetch_related("districts").get(id=c.id)
|
|
|
|
self.assertEqual(city.districts.count(), 1)
|
|
|
|
city.districts.add(d2)
|
|
|
|
self.assertEqual(city.districts.count(), 2)
|
|
|
|
city.districts.remove(d2)
|
|
|
|
self.assertEqual(city.districts.count(), 1)
|
2017-09-21 12:27:04 +08:00
|
|
|
|
|
|
|
def test_cached_relation_invalidated_on_save(self):
|
|
|
|
"""
|
|
|
|
Model.save() invalidates stale ForeignKey relations after a primary key
|
|
|
|
assignment.
|
|
|
|
"""
|
|
|
|
self.assertEqual(self.a.reporter, self.r) # caches a.reporter
|
|
|
|
self.a.reporter_id = self.r2.pk
|
|
|
|
self.a.save()
|
|
|
|
self.assertEqual(self.a.reporter, self.r2)
|
2018-10-29 10:54:02 +08:00
|
|
|
|
|
|
|
def test_cached_foreign_key_with_to_field_not_cleared_by_save(self):
|
|
|
|
parent = Parent.objects.create(name="a")
|
|
|
|
child = ToFieldChild.objects.create(parent=parent)
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertIs(child.parent, parent)
|
2018-11-01 13:10:29 +08:00
|
|
|
|
|
|
|
def test_reverse_foreign_key_instance_to_field_caching(self):
|
|
|
|
parent = Parent.objects.create(name="a")
|
|
|
|
ToFieldChild.objects.create(parent=parent)
|
|
|
|
child = parent.to_field_children.get()
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertIs(child.parent, parent)
|
2020-02-18 17:50:19 +08:00
|
|
|
|
|
|
|
def test_add_remove_set_by_pk_raises(self):
|
|
|
|
usa = Country.objects.create(name="United States")
|
|
|
|
chicago = City.objects.create(name="Chicago")
|
|
|
|
msg = "'City' instance expected, got %s" % chicago.pk
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
usa.cities.add(chicago.pk)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
usa.cities.remove(chicago.pk)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
usa.cities.set([chicago.pk])
|