Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning values of a 'ReturnList' without the key in DjangoRestFramework

So I have this Serializers

class MovieSerializer(serializers.ModelSerializer):
    class Meta:
        model = Movie
        fields = ('name', 'thumbnail', 'source_url', 'duration')

class SingleCategorySerializer(serializers.ModelSerializer):
    movies = MovieSerializer(many=True, read_only=True)

    class Meta:
        model = MoviesCategories
        fields = ('movies',)

The Movie model has a relationship with the movies categories as follows

category = models.ForeignKey(MoviesCategories, related_name="movies", on_delete=models.DO_NOTHING)

Now when I try this serializer = SingleCategorySerializer(movie_category, many=True), the result of serializer.data is

[
{
    "movies": [
        {
            "name": "Guardians of the Galaxy",
            "thumbnail": "https://nerdist.com/wp-content/uploads/2016/08/Guardians-of-the-Galaxy-poster-2.jpg",
            "source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
            "duration": "05:30:09"
        },
        {
            "name": "Star Wars II",
            "thumbnail": "https://images-na.ssl-images-amazon.com/images/I/51TEG6X589L.jpg",
            "source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
            "duration": "05:32:26"
        }
    ]
}
]

I only want the values of the movies to be returned. Like this

[
    {
        "name": "Guardians of the Galaxy",
        "thumbnail": "https://nerdist.com/wp-content/uploads/2016/08/Guardians-of-the-Galaxy-poster-2.jpg",
        "source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
        "duration": "05:30:09"
    },
    {
        "name": "Star Wars II",
        "thumbnail": "https://images-na.ssl-images-amazon.com/images/I/51TEG6X589L.jpg",
        "source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
        "duration": "05:32:26"
    }
]

Any help will be appreciated.

like image 840
Esir Kings Avatar asked Jan 26 '26 12:01

Esir Kings


1 Answers

According to your post , serializer.data returns

[
{
    "movies": [
        {
            "name": "Guardians of the Galaxy",
            "thumbnail": "https://nerdist.com/wp-content/uploads/2016/08/Guardians-of-the-Galaxy-poster-2.jpg",
            "source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
            "duration": "05:30:09"
        },
        {
            "name": "Star Wars II",
            "thumbnail": "https://images-na.ssl-images-amazon.com/images/I/51TEG6X589L.jpg",
            "source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
            "duration": "05:32:26"
         }
    ]
}
]

use simple list indexing. serializer.data returns a list with one element. So to get that you would call serializer.data[0]. That element is a dictionary. So to access that, just use serializer.data[0]["movies"]. This says, "hey python, I want the first element of this list (serializer.data), and from that element....OOH, its a dict. Can I have the item with a key of "movies". "

like image 173
Michael Ilie Avatar answered Jan 29 '26 02:01

Michael Ilie