mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #16281 -- Fixed ContentType.get_object_for_this_type() in a multiple database setup.
This commit is contained in:
committed by
Mariusz Felisiak
parent
a47de0d6cd
commit
02a600ff67
@@ -1302,6 +1302,34 @@ class QueryTestCase(TestCase):
|
||||
title="Dive into Water", published=datetime.date(2009, 5, 4), extra_arg=True
|
||||
)
|
||||
|
||||
@override_settings(DATABASE_ROUTERS=["multiple_database.tests.TestRouter"])
|
||||
def test_contenttype_in_separate_db(self):
|
||||
ContentType.objects.using("other").all().delete()
|
||||
book_other = Book.objects.using("other").create(
|
||||
title="Test title other", published=datetime.date(2009, 5, 4)
|
||||
)
|
||||
book_default = Book.objects.using("default").create(
|
||||
title="Test title default", published=datetime.date(2009, 5, 4)
|
||||
)
|
||||
book_type = ContentType.objects.using("default").get(
|
||||
app_label="multiple_database", model="book"
|
||||
)
|
||||
|
||||
book = book_type.get_object_for_this_type(title=book_other.title)
|
||||
self.assertEqual(book, book_other)
|
||||
book = book_type.get_object_for_this_type(using="other", title=book_other.title)
|
||||
self.assertEqual(book, book_other)
|
||||
|
||||
with self.assertRaises(Book.DoesNotExist):
|
||||
book_type.get_object_for_this_type(title=book_default.title)
|
||||
book = book_type.get_object_for_this_type(
|
||||
using="default", title=book_default.title
|
||||
)
|
||||
self.assertEqual(book, book_default)
|
||||
|
||||
all_books = book_type.get_all_objects_for_this_type()
|
||||
self.assertCountEqual(all_books, [book_other])
|
||||
|
||||
|
||||
class ConnectionRouterTestCase(SimpleTestCase):
|
||||
@override_settings(
|
||||
|
||||
Reference in New Issue
Block a user