Does the underlying Author model have a books attribute? If not, this may
be the cause. It may default to book_set, or something, but you can rename
it to books when you define the Author model, we do this in Social, i.e. a
Message has a posts attribute, not a post_set attribute.


On Sat, Feb 22, 2014 at 12:13 AM, Andy Matthews <notifications@github.com>wrote:

> In article.md:
>
> > @@ -134,6 +136,7 @@ While this API view is pretty slick, it's a one for one with the database. Let's
> >  ```
> >  books = serializers.SerializerMethodField('get_books')
> >  ```
> > +[Steve: I believe there's an even easier way to serialize related models, using DRF's nested relationships, http://www.django-rest-framework.org/api-guide/relations#nested-relationships]
>
> @swinton <https://github.com/swinton> I've tried replacing the get_book
> method with a nested serializer and for some reason it keeps throwing an
> error:
>
> Author object has no attribute books
>
> Added a BookSerializer:
>
> class BookSerializer(serializers.ModelSerializer):
>     """
>     Serializing all the Books
>     """
>     class Meta:
>         model = Book
>         fields = ('id', 'title', 'isbn', 'author')
>
> And changed this line:
>
> books = serializers.SerializerMethodField('get_books')
>
> to this:
>
> books = BookSerializer(many=True)
>
> --
> Reply to this email directly or view it on GitHub<https://github.com/commadelimited/beginners-guide-to-django-rest-framework/pull/1/files#r9970207>
> .
>



-- 
Steve Winton
about.me/swinton
twitter.com/steveWINton
linkedin.com/in/stephenwinton