-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Support usage of 'source' in extra_kwargs
.
#4688
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
Conversation
This commit fixes the issue when you set the keyword argument `source` and your have not set the serializer fields explicitly. Then the construction of field failed because there is not actually any model field with that name. However, you are still able to imply the name of model field by providing the `source` keyword argument.
Hi, I think a test case here would help understand what this change brings to the table. |
Imagine the following scenario: class MyModel(models.Model):
model_field_name = models.CharField(max_length=255) class MySerializer(serializers.ModelSerializer):
class Meta(object):
fields = ['api_field_name']
extra_kwargs = {
'api_field_name': {
'source': 'model_field_name'
}
}
model = models.MyModel This code will break down with the following exception.
My commit fixes this issue so that I will no longer need to specify the class MySerializer(serializers.ModelSerializer):
api_field_name = serializers.CharField(source='model_field_name')
class Meta(object):
fields = ['api_field_name']
model = models.MyModel |
This commit fixes the issue when you set the keyword argument `source` and your have not set the serializer fields explicitly. Then the construction of field failed because there is not actually any model field with that name. However, you are still able to imply the name of model field by providing the `source` keyword argument at `Meta.extra_kwargs`. For more information, see: encode/django-rest-framework#4688
What about this? |
Yup, seems perfectly reasonable. |
extra_kwargs
.
This doesn't seem to work for following related fields - I cannot do
Does this seem correct? In my testing, an explicit field attribute was required |
The |
@xordoquy @tomchristie It would still be very reasonable and useful for a lot of cases. For instance in many-to-many serialization, we provide the following data: {
"id": "<trhough_model_id>",
"some_field": "<field_on_many_to_many>",
"object_id": "<related_field_of_m2m>" # <-- currently this serialization cannot be done using `extras`
} In Python it will look like: class M2MSerializer(ModelSerializer[M2MModel]):
# object_id = CharField(source='related__external_id', help_text='...') # <-- we are trying to avoid this one
class Meta:
extra_fields = {
'id': {'source': 'external_id', 'help_text': '...'},
'object_id': {'source': 'related__external_id', 'help_text': '...'}, # <-- this is not working and we have to create the object manually and we should keep it in sync also manually
'some_field': {'source': 'another_field', 'help_text': '...'},
}
fields = ('id', 'object_id', 'some_field') |
you can come with a failing test case in a PR |
This commit fixes the issue when you set the keyword argument
source
and your have not set the serializer fields explicitly. Then the
construction of field failed because there is not actually any model
field with that name.
However, you are still able to imply the name of model field by
taking advantage of the
source
keyword argument.