Type error object is not JSON serializable when using ChoiceField

Viewed 6432

I am creating a REST API in Django using the Django Rest framework and cannot use any other libraries or plugins. I have been having an issue the past few days which I am having trouble solving.

In my seralizer.py I have the following

class BookSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.ReadOnlyField(source = 'owner.username')
    genres = serializers.ChoiceField(choices=Genre.objects.values_list())
    # genres = GenreSerializer()

class Meta:
    model = VideoGame
    fields = ('title', 'description', 'brief', 'genres', 'owner') 

I would like it so when users access the GUI and attempt to make a new book, they can select a genre from a choicefield, this is then translated into its ID. If users access via the rest API, they must also enter the ID.

When the database is blank, I am able to successfully post a new book to the server and select the dropdown using the choicefield. But when the database has an entry, I get the error below. This is solved when i replace the choicefield with the serializers. However then I am left with an input field and not a choice field. Does anyone know how I can solve this?

TypeError at /book/
<Genre: Genre object> is not JSON serializable
2 Answers
Related