Skip to content

More documentation on source='*' for Serializer fields #2032

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
cancan101 opened this issue Nov 6, 2014 · 3 comments · Fixed by #5688
Closed

More documentation on source='*' for Serializer fields #2032

cancan101 opened this issue Nov 6, 2014 · 3 comments · Fixed by #5688

Comments

@cancan101
Copy link
Contributor

Right now the docs for the special value just say:

The value source='*' has a special meaning, and is used to indicate that the entire object should be passed through to the field. This can be useful for creating nested representations. (See the implementation of the PaginationSerializer class for an example.)

And as far as the PaginationSerializer, the entire implementation is just this.

What exactly does the * do and what would happen in the case of PaginationSerializer if it were omitted?

@tomchristie
Copy link
Member

Yup totally agree we should have fuller documentation on source="*".

Not too fussed about that in the particular context of PaginationSerializer as we'll be completely revisiting the pagination API for 3.1.

Here's an example of using the source='*'

Say we have a model like so:

class UserInfoModel(model.Model):
    address_line1 = ...
    address_line2 = ...
    town = ...
    country = ...
    post_code = ...
    phone = ...
    email = ...

Now say that we wanted to split out the 'phone' and 'email' from the rest of the representation and end up with an output that looks like this:

{
    "address": {"address_line1": ..., "address_line2":..., ...}
    "contact_details": {"email": ..., "phone_number": ...}
}

We can do so by using an AddressSerializer and ContactDetailsSerializer, and passing them the complete UserInfoModel instance.

class UserInfoSerializer(serializers.Serializer)
    address = AddressSerializer(source='*')
    contact_details = ContactDetailsSerializer(source='*')

This serializer would also handle reconstructing updates from the split dictionary style, restoring all the attributes into a single dictionary that can be used to create or update a UserInfoModel instance.

And yes, more docs on this.

@ssbrewster
Copy link

More examples are always useful but would you then want to add an example for each of the serializer field core arguments (currently there is an example for the style argument only)?

@tomchristie
Copy link
Member

@ssbrewster If someone wants to take it on, then yes. Otherwise, not a huge priority.

carltongibson added a commit to carltongibson/django-rest-framework that referenced this issue Dec 19, 2017
carltongibson added a commit to carltongibson/django-rest-framework that referenced this issue Dec 19, 2017
carltongibson added a commit that referenced this issue Dec 20, 2017
* Add example using `source=‘*’` to custom field docs.
* Add nested serialiser example

Closes #2032 closes #3066
pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this issue Nov 17, 2020
* Add example using `source=‘*’` to custom field docs.
* Add nested serialiser example

Closes encode#2032 closes encode#3066
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants