diff options
| author | rimi0108 <hyerimc858@gmail.com> | 2025-10-04 13:52:59 +0900 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-12-03 15:04:52 -0500 |
| commit | 93540b34d4ef46f68df2c8bfe90447d0f649a852 (patch) | |
| tree | 93967647a5590ec653372f38cfb7123166d819c3 /tests | |
| parent | ddb7236b0ae9be3163c90f799fb79396e9f61cc8 (diff) | |
Fixed #35729 -- Enabled natural key serialization opt-out for subclasses.
Refactored serialization logic to allow models inheriting a natural_key()
method (e.g. AbstractBaseUser) to explicitly opt out of natural key
serialization by returning an empty tuple from the method.
Thanks Jonas Dittrich for the report.
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/serializers/models/natural.py | 17 | ||||
| -rw-r--r-- | tests/serializers/test_natural.py | 35 |
2 files changed, 52 insertions, 0 deletions
diff --git a/tests/serializers/models/natural.py b/tests/serializers/models/natural.py index cfc57b1127..f32d781a76 100644 --- a/tests/serializers/models/natural.py +++ b/tests/serializers/models/natural.py @@ -2,6 +2,7 @@ import uuid +from django.contrib.auth.base_user import AbstractBaseUser from django.db import models @@ -74,3 +75,19 @@ class FKAsPKNoNaturalKey(models.Model): def natural_key(self): raise NotImplementedError("This method was not expected to be called.") + + +class SubclassNaturalKeyOptOutUser(AbstractBaseUser): + email = models.EmailField(unique=False, null=True, blank=True) + USERNAME_FIELD = "email" + + def natural_key(self): + return () + + +class PostToOptOutSubclassUser(models.Model): + author = models.ForeignKey(SubclassNaturalKeyOptOutUser, on_delete=models.CASCADE) + title = models.CharField(max_length=100) + subscribers = models.ManyToManyField( + SubclassNaturalKeyOptOutUser, related_name="subscribed_posts", blank=True + ) diff --git a/tests/serializers/test_natural.py b/tests/serializers/test_natural.py index b5b35708c6..322abeb7fc 100644 --- a/tests/serializers/test_natural.py +++ b/tests/serializers/test_natural.py @@ -9,6 +9,8 @@ from .models import ( NaturalKeyAnchor, NaturalKeyThing, NaturalPKWithDefault, + PostToOptOutSubclassUser, + SubclassNaturalKeyOptOutUser, ) from .tests import register_tests @@ -250,6 +252,34 @@ def fk_as_pk_natural_key_not_called(self, format): self.assertEqual(obj.object.pk, o1.pk) +def natural_key_opt_out_test(self, format): + """ + When a subclass of AbstractBaseUser opts out of natural key serialization + by returning an empty tuple, both FK and M2M relations serialize as + integer PKs and can be deserialized without error. + """ + user1 = SubclassNaturalKeyOptOutUser.objects.create(email="user1@example.com") + user2 = SubclassNaturalKeyOptOutUser.objects.create(email="user2@example.com") + + post = PostToOptOutSubclassUser.objects.create( + author=user1, title="Post 2 (Subclass Opt-out)" + ) + post.subscribers.add(user1, user2) + + user_data = serializers.serialize(format, [user1], use_natural_primary_keys=True) + post_data = serializers.serialize(format, [post], use_natural_foreign_keys=True) + + list(serializers.deserialize(format, user_data)) + deserialized_posts = list(serializers.deserialize(format, post_data)) + + post_obj = deserialized_posts[0].object + self.assertEqual(user1.email, post_obj.author.email) + self.assertEqual( + sorted([user1.email, user2.email]), + sorted(post_obj.subscribers.values_list("email", flat=True)), + ) + + # Dynamically register tests for each serializer register_tests( NaturalKeySerializerTests, @@ -284,3 +314,8 @@ register_tests( "test_%s_fk_as_pk_natural_key_not_called", fk_as_pk_natural_key_not_called, ) +register_tests( + NaturalKeySerializerTests, + "test_%s_natural_key_opt_out", + natural_key_opt_out_test, +) |
