Thank you for the response.
Yes, I have tried this way too (make two serializers and override the 
get_serializer_class).
But, we have to make more than one serializer and it will be more 
complicated if the album not only have tracks relation in it.
I think if we can add a feature like 
`album/1/?include=relation1,relation2`, then we can dynamically choose 
which relation to include.


On Friday, March 8, 2019 at 11:58:25 AM UTC+7, Fabio Andrés García Sánchez 
wrote:
>
> You can do it using two different AlbumSerializers. One of them without 
> tracks.
> Then, in the view, overriding the method get_serializer you can get the 
> parameter include with tracks and then:
>
> If has psrameter:
>     self.serializer_class=AlbumSerializersWithTraks
> else:
>     self.serializer_class=AlbumSerializers
>
> In that way, you will change your view.
>
> I apologize I did not give you more code, in writing from my cellphone.
>
> On Thu, Mar 7, 2019 at 11:48 PM Krisnadi Krisnadi <alexande...@gmail.com 
> <javascript:>> wrote:
>
>> Is there any simple and common way to state which nested relationship 
>> object you want to include in the response?
>> for example, I have this serializer:
>>
>> class TrackSerializer(serializers.ModelSerializer):
>>     class Meta:
>>         model = Track
>>         fields = '__all__'
>> class AlbumSerializer(serializers.ModelSerializer):
>>     tracks = TrackSerializer(many=True, read_only=True)
>>
>>     class Meta:
>>         model = Album
>>         fields = '__all__'
>>
>> when I access album detail endpoint, I get this response:
>>
>> {
>>     'album_name': 'The Grey Album',
>>     'artist': 'Danger Mouse',
>>     'tracks': [
>>         {'order': 1, 'title': 'Public Service Announcement', 'duration': 
>> 245},
>>         {'order': 2, 'title': 'What More Can I Say', 'duration': 264},
>>         {'order': 3, 'title': 'Encore', 'duration': 159},
>>         ...
>>     ],}
>>
>> The tracks object are included automatically in the response. Is there 
>> any option like this,localhost:8000/album/1/?include=tracks so the 
>> tracks data only appear when it stated on the include param. I also make 
>> a question on stackoverflow  
>> <https://stackoverflow.com/questions/55046594/django-rest-framework-optional-nested-relationship>about
>>  
>> this. But it seems there are not any standardized way to do it.
>> Is it possible to make this as a new feature or third-party package? If 
>> it is not, what are the criteria to make a new third-party package for drf?
>> Thank you.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-rest-framework+unsubscr...@googlegroups.com <javascript:>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> -- 
> Fabio Andrés García Sánchez
> Ingeniero de Sistemas
> Administrador de Empresas
> fabio.garc...@gmail.com <javascript:>
> Celular +57 311 3600919
> Universidad ICESI
>
> [image: Ver mi perfil]Ver Perfil <http://co.linkedin.com/in/fabgarsan>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to