-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add support for source with attributes in extra_kwargs of ModelSerializer #9077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dd0bb97
705e277
c1ccc37
a5b239e
2fc08b2
05b2e3b
b4ce3e5
53141ac
d60b909
84fd5ee
5be47e4
5a106a0
af69a58
998025a
f15196a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1101,9 +1101,35 @@ def get_fields(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if source == '*': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| source = field_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Get the right model and info for source with attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| source_attrs = source.split('.') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| source_info = info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| source_model = model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| attr_info = info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| attr_model = model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for attr in source_attrs[:-1]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if attr not in attr_info.relations: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| attr_model = attr_info.relations[attr].related_model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1113
to
+1116
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if attr not in attr_info.relations: | |
| break | |
| attr_model = attr_info.relations[attr].related_model | |
| relation_info = attr_info.relations.get(attr) | |
| if relation_info is None: | |
| break | |
| if getattr(relation_info, 'to_many', False): | |
| # Do not rewrite sources that traverse to-many relations. | |
| break | |
| attr_model = relation_info.related_model |
Copilot
AI
Apr 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic runs for every auto-generated field, even when source has no dotted path. You can avoid the extra split()/loop and repeated get_field_info() calls by guarding the new traversal with something like if '.' in source: (or if len(source_attrs) > 1: after splitting).
| source_attrs = source.split('.') | |
| source_info = info | |
| source_model = model | |
| attr_info = info | |
| attr_model = model | |
| for attr in source_attrs[:-1]: | |
| if attr not in attr_info.relations: | |
| break | |
| attr_model = attr_info.relations[attr].related_model | |
| attr_info = model_meta.get_field_info(attr_model) | |
| else: | |
| attr = source_attrs[-1] | |
| if ( | |
| attr in attr_info.fields_and_pk | |
| or attr in attr_info.relations | |
| or hasattr(attr_model, attr) | |
| or attr == self.url_field_name | |
| ): | |
| source = attr | |
| source_info = attr_info | |
| source_model = attr_model | |
| source_info = info | |
| source_model = model | |
| if '.' in source: | |
| source_attrs = source.split('.') | |
| attr_info = info | |
| attr_model = model | |
| for attr in source_attrs[:-1]: | |
| if attr not in attr_info.relations: | |
| break | |
| attr_model = attr_info.relations[attr].related_model | |
| attr_info = model_meta.get_field_info(attr_model) | |
| else: | |
| attr = source_attrs[-1] | |
| if ( | |
| attr in attr_info.fields_and_pk | |
| or attr in attr_info.relations | |
| or hasattr(attr_model, attr) | |
| or attr == self.url_field_name | |
| ): | |
| source = attr | |
| source_info = attr_info | |
| source_model = attr_model |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||
| from django.contrib.auth.models import User | ||||||||||||||||||||||||||||||||
| from django.core.exceptions import ImproperlyConfigured | ||||||||||||||||||||||||||||||||
| from django.core.serializers.json import DjangoJSONEncoder | ||||||||||||||||||||||||||||||||
| from django.core.validators import ( | ||||||||||||||||||||||||||||||||
|
|
@@ -726,6 +727,42 @@ class Meta: | |||||||||||||||||||||||||||||||
| """) | ||||||||||||||||||||||||||||||||
| self.assertEqual(repr(TestSerializer()), expected) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def test_source_with_attributes(self): | ||||||||||||||||||||||||||||||||
| class UserProfile(models.Model): | ||||||||||||||||||||||||||||||||
| age = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(100)]) | ||||||||||||||||||||||||||||||||
| birthdate = models.DateField() | ||||||||||||||||||||||||||||||||
| user = models.ForeignKey(User, on_delete=models.CASCADE) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class UserProfileSerializer(serializers.ModelSerializer): | ||||||||||||||||||||||||||||||||
| class Meta: | ||||||||||||||||||||||||||||||||
| model = UserProfile | ||||||||||||||||||||||||||||||||
| fields = ('username', 'email', 'first_name', 'last_name', 'age', 'birthdate') | ||||||||||||||||||||||||||||||||
| extra_kwargs = { | ||||||||||||||||||||||||||||||||
| 'username': { | ||||||||||||||||||||||||||||||||
| 'source': 'user.username', | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| 'email': { | ||||||||||||||||||||||||||||||||
| 'source': 'user.email', | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| 'first_name': { | ||||||||||||||||||||||||||||||||
| 'source': 'user.first_name', | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| 'last_name': { | ||||||||||||||||||||||||||||||||
| 'source': 'user.last_name', | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| expected = dedent(""" | ||||||||||||||||||||||||||||||||
| UserProfileSerializer(): | ||||||||||||||||||||||||||||||||
| username = CharField(help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, source='user.username', validators=[<django.contrib.auth.validators.UnicodeUsernameValidator object>, <UniqueValidator(queryset=User.objects.all())>]) | ||||||||||||||||||||||||||||||||
| email = EmailField(allow_blank=True, label='Email address', max_length=254, required=False, source='user.email') | ||||||||||||||||||||||||||||||||
| first_name = CharField(allow_blank=True, max_length=150, required=False, source='user.first_name') | ||||||||||||||||||||||||||||||||
| last_name = CharField(allow_blank=True, max_length=150, required=False, source='user.last_name') | ||||||||||||||||||||||||||||||||
| age = IntegerField(max_value=100, min_value=1) | ||||||||||||||||||||||||||||||||
| birthdate = DateField() | ||||||||||||||||||||||||||||||||
| """) | ||||||||||||||||||||||||||||||||
| self.assertEqual(repr(UserProfileSerializer()), expected) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
+765
to
766
|
||||||||||||||||||||||||||||||||
| def test_source_with_to_many_raises_improperly_configured(self): | |
| class UserProfile(models.Model): | |
| user = models.ForeignKey(User, on_delete=models.CASCADE) | |
| class InvalidUserProfileSerializer(serializers.ModelSerializer): | |
| groups = serializers.CharField(source='user.groups.name') | |
| class Meta: | |
| model = UserProfile | |
| fields = ('groups',) | |
| with self.assertRaises(ImproperlyConfigured): | |
| InvalidUserProfileSerializer() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new example uses dotted sources to pull fields from the related
user. By default,ModelSerializer.create()/update()does not support writable dotted-source fields (it asserts unless you setread_only=Trueor implement explicit create/update handling). It’d help to either mark these example fields asread_only=True(viaextra_kwargs) or add a short note clarifying the write behavior.