[Solved]-Django REST framework: AttributeError: 'User' object has no attribute 'books'

18👍

You have not specified the related_name in the ManyToManyField. By default it will be book_set. Therefore you can do:

book_set = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

If you want to use books in the serializers, you can do this in the Book model:

owner = models.ManyToManyField(User, related_name="books")

Leave a comment