1

I'm triying to create a Custom URL in a nested serializer. This is my serializer.py ->

class SerieSerializer(serializers.HyperlinkedModelSerializer):
created_by = serializers.ReadOnlyField(source='created_by.username')
picture = serializers.ImageField()

class Meta:
    model = Serie
    fields = ('url', 'name', 'genre', 'director', 'release', 'seasons', 'review', 'picture', 'actors', 'created_by')

and this is the result:

[
{
    "url": "http://localhost:8000/series/1/",
    "name": "Mr. Robot",
    "genre": "DRAMA",
    "director": "Sam Esmail",
    "release": "2015-06-24",
    "seasons": 2,
    "review": "Mr. Robot es una serie de televisión estadounidense creada por Sam Esmail. Se estrenó el 24 de junio de 2015 en la cadena USA Network. El mismo día la serie se renovó para una segunda temporada, estrenada el 13 de julio de 2016. El 16 de agosto del mismo año Mr. Robot fue renovado para una tercera temporada a estrenarse en 2017.",
    "picture": "http://res.cloudinary.com/dqohbm9y4/image/upload/v1487190362/uasfb8maqzh9urp4bvrd.png",
    "actors": [
        "http://localhost:8000/actors/3/",
        "http://localhost:8000/actors/4/"
    ],
    "created_by": "mosthated"
}
]

But I need something like this, in "Actors":

[
{
    "url": "http://localhost:8000/series/1/",
    "name": "Mr. Robot",
    "genre": "DRAMA",
    "director": "Sam Esmail",
    "release": "2015-06-24",
    "seasons": 2,
    "review": "Mr. Robot es una serie de televisión estadounidense creada por Sam Esmail. Se estrenó el 24 de junio de 2015 en la cadena USA Network. El mismo día la serie se renovó para una segunda temporada, estrenada el 13 de julio de 2016. El 16 de agosto del mismo año Mr. Robot fue renovado para una tercera temporada a estrenarse en 2017.",
    "picture": "http://res.cloudinary.com/dqohbm9y4/image/upload/v1487190362/uasfb8maqzh9urp4bvrd.png",
    "actors": [
        "Rami Malek": "http://localhost:8000/actors/3/",
        "Christian Slater": "http://localhost:8000/actors/4/"
    ],
    "created_by": "mosthated"
}
]

I need this to send the profile to call for example: go to "Rami Malek" profile with your name as URL, or display it and display the URL with your primary key.

If you can help me, thank so much.

1 Answer 1

1

Create a customActorSerializer like below and link it with SerieSerializer.

class ActorSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Actor
        fields = ('url','actor_name')

class SerieSerializer(serializers.HyperlinkedModelSerializer):
    created_by = serializers.ReadOnlyField(source='created_by.username')
    picture = serializers.ImageField()
    actors = ActorSerializer(many=True)

    class Meta:
        model = Serie
        fields = ('url', 'name', 'genre', 'director', 'release', 'seasons', 'review', 'picture', 'actors', 'created_by')

hope it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.