MagiciaN
MagiciaN

Reputation: 93

Django REST Serializer, show an exact value

Really I did an almost 5-hours research and could not find something that will work the way I want.

The question is just a simple one, I suppose. I wanna build a REST Django framework for Game, with serializers etc.

When I try to ask for "Genres"(Game Genres), the JSON return this: Genre Serializer

Ideally, I just want to return only the values of the game genres without the annoying "GenreTitle", everywhere.

My model:

class Genre(models.Model):
   GenreTitle = models.CharField(max_length=30,verbose_name = 'Title')
   GenreDescription = models.TextField(max_length=500,verbose_name = 'Description')
   GenreImage = models.ImageField(null=True, verbose_name='Image')

def __str__(self):
    return self.GenreTitle

My serializer:

class GenreSerializer(serializers.ModelSerializer):
   class Meta:
       model= Genre
       fields=('GenreTitle',)

I know that is a pice of cake for Django developers, but i struggle much because I am beginner at this.

Thanks in advance!

Upvotes: 0

Views: 109

Answers (1)

vishes_shell
vishes_shell

Reputation: 23514

Everything is already beautifully documented in django rest docs

You just need to include needed fields, If you want all fields just do so:

class GenreSerializer(serializers.ModelSerializer):
    class Meta:
        model = Genre
        fields = '__all__'

If you need everything without annoying GenreTitle then exclude it

class GenreSerializer(serializers.ModelSerializer):
    class Meta:
        model = Genre
        exclude = ('GenreTitle',)

If you want specifics:

class GenreSerializer(serializers.ModelSerializer):
    class Meta:
        model = Genre
        fields = ('GenreTitle', 'GenreDescription', 'GenreImage')

Upvotes: 1

Related Questions