mirror of
https://github.com/django/django.git
synced 2025-04-01 12:06:43 +00:00
Fixed #33582 -- Fixed deserializing natural keys with foreing key dependencies in a multiple database setup.
This commit is contained in:
parent
ed6db53542
commit
4b8e4f5060
@ -336,7 +336,9 @@ def build_instance(Model, data, db):
|
|||||||
and hasattr(default_manager, "get_by_natural_key")
|
and hasattr(default_manager, "get_by_natural_key")
|
||||||
and hasattr(Model, "natural_key")
|
and hasattr(Model, "natural_key")
|
||||||
):
|
):
|
||||||
natural_key = Model(**data).natural_key()
|
obj = Model(**data)
|
||||||
|
obj._state.db = db
|
||||||
|
natural_key = obj.natural_key()
|
||||||
try:
|
try:
|
||||||
data[Model._meta.pk.attname] = Model._meta.pk.to_python(
|
data[Model._meta.pk.attname] = Model._meta.pk.to_python(
|
||||||
default_manager.db_manager(db).get_by_natural_key(*natural_key).pk
|
default_manager.db_manager(db).get_by_natural_key(*natural_key).pk
|
||||||
|
15
tests/fixtures_regress/fixtures/nk_with_foreign_key.json
Normal file
15
tests/fixtures_regress/fixtures/nk_with_foreign_key.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"model": "fixtures_regress.person",
|
||||||
|
"fields": {
|
||||||
|
"name": "J.R.R. Tolkien"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"model": "fixtures_regress.naturalkeywithfkdependency",
|
||||||
|
"fields": {
|
||||||
|
"name": "The Lord of the Rings",
|
||||||
|
"author": ["J.R.R. Tolkien"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
@ -147,6 +147,26 @@ class Book(models.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NaturalKeyWithFKDependencyManager(models.Manager):
|
||||||
|
def get_by_natural_key(self, name, author):
|
||||||
|
return self.get(name=name, author__name=author)
|
||||||
|
|
||||||
|
|
||||||
|
class NaturalKeyWithFKDependency(models.Model):
|
||||||
|
name = models.CharField(max_length=255)
|
||||||
|
author = models.ForeignKey(Person, models.CASCADE)
|
||||||
|
|
||||||
|
objects = NaturalKeyWithFKDependencyManager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = ["name", "author"]
|
||||||
|
|
||||||
|
def natural_key(self):
|
||||||
|
return (self.name,) + self.author.natural_key()
|
||||||
|
|
||||||
|
natural_key.dependencies = ["fixtures_regress.Person"]
|
||||||
|
|
||||||
|
|
||||||
class NKManager(models.Manager):
|
class NKManager(models.Manager):
|
||||||
def get_by_natural_key(self, data):
|
def get_by_natural_key(self, data):
|
||||||
return self.get(data=data)
|
return self.get(data=data)
|
||||||
|
@ -44,6 +44,7 @@ from .models import (
|
|||||||
M2MSimpleCircularA,
|
M2MSimpleCircularA,
|
||||||
M2MSimpleCircularB,
|
M2MSimpleCircularB,
|
||||||
M2MThroughAB,
|
M2MThroughAB,
|
||||||
|
NaturalKeyWithFKDependency,
|
||||||
NKChild,
|
NKChild,
|
||||||
Parent,
|
Parent,
|
||||||
Person,
|
Person,
|
||||||
@ -791,6 +792,25 @@ class NaturalKeyFixtureTests(TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NaturalKeyFixtureOnOtherDatabaseTests(TestCase):
|
||||||
|
databases = {"other"}
|
||||||
|
|
||||||
|
def test_natural_key_dependencies(self):
|
||||||
|
"""
|
||||||
|
Natural keys with foreing keys in dependencies works in a multiple
|
||||||
|
database setup.
|
||||||
|
"""
|
||||||
|
management.call_command(
|
||||||
|
"loaddata",
|
||||||
|
"nk_with_foreign_key.json",
|
||||||
|
database="other",
|
||||||
|
verbosity=0,
|
||||||
|
)
|
||||||
|
obj = NaturalKeyWithFKDependency.objects.using("other").get()
|
||||||
|
self.assertEqual(obj.name, "The Lord of the Rings")
|
||||||
|
self.assertEqual(obj.author.name, "J.R.R. Tolkien")
|
||||||
|
|
||||||
|
|
||||||
class M2MNaturalKeyFixtureTests(TestCase):
|
class M2MNaturalKeyFixtureTests(TestCase):
|
||||||
"""Tests for ticket #14426."""
|
"""Tests for ticket #14426."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user