Thank you. I think i can try to use this answer.
On Friday, March 8, 2019 at 12:19:19 PM UTC+7, SuryaKumar K wrote:
>
> Better you can use serializer's inbuild remove_fields parameter where
> you can pass the field name as argument if you want to remove.
>
> *ex:*
>
> if tracks_needed:
>
>
OK i understand. Thank you very much for your help.
On Friday, March 8, 2019 at 12:16:22 PM UTC+7, Fabio Andrés García Sánchez
wrote:
>
> Why not creating a BaseSerializer call AlbumSerializers and then, just
> create AlbumSerializersWithTrack(AlbumSerializers)?
> You will have another
Better you can use serializer's inbuild remove_fields parameter where you
can pass the field name as argument if you want to remove.
*ex:*
if tracks_needed:
AlbumSerializer(data)
else:
AlbumSerializer(data, remove_fields=['tracks'])
##33
It not a perfect
Why not creating a BaseSerializer call AlbumSerializers and then, just
create AlbumSerializersWithTrack(AlbumSerializers)?
You will have another Serializer based in AlbumSerializers.
What you have to do is just to override tracks=Tracks
Serializer(many=true), but in the BaseSerialer
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
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:
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