Python Django REST call returning object number instead of object name
<p>I'm new to Python, and I guess I'm serializing it incorrectly.</p>

<p><strong>This is the REST call result:</strong>
<a href="http://i.stack.imgur.com/SXko7.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/SXko7.jpg" alt="enter image description here"></a></p>

<blockquote>
  <p><strong>authors/models.py:</strong></p>
</blockquote>

<pre><code>    from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class Author(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return self.name


class Book(models.Model):
    auto_increment_id = models.AutoField(primary_key=True)
    name = models.CharField('Book name', max_length=100)
    author = models.ForeignKey(Author, blank=False, null=False, related_name='book_author')
    contents = models.TextField('Contents', blank=False, null=False)
    def __str__(self):
        return self.name
</code></pre>

<blockquote>
  <p><strong>authors/views.py</strong></p>
</blockquote>

<pre><code>from authors.models import Book, Author
from authors.serializers import BookSerializer, AuthorSerializer
from django.shortcuts import render
from rest_framework import generics


class ListCreateBooks(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

class ListCreateAuthor(generics.ListCreateAPIView):
    queryset = Author.objects.all()
    serializer_class = AuthorSerializer
</code></pre>

<blockquote>
  <p><strong>authors/serializers.py</strong></p>
</blockquote>

<pre><code>from authors.models import Book, Author
from rest_framework import serializers

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ('name', 'author')

class AuthorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Author
        book_author = 'name'
</code></pre>

<p>I'm new to Django, but I tried many things, in my views.py I've added another class called <code>AuthorSerializer</code> importing from a corresponding class I created in serializers, but then I realized that I have no clue how to add my <code>ListCreateAuthor</code> to:</p>

<pre><code> url(r'^api-auth/', ListCreateBooks.as_view(), name='list_books')
</code></pre>

<p>I've added another parameter with <code>ListCreateAuthor.as_view()</code> that gave me an immediate error (and which also didn't make much sense) Am I going the wrong way here and how can I solve this?</p>

<blockquote>
  <p><strong>EDIT:</strong>  @Abdulafaja gave a partial answer, which did solve the read, but now after checking a POST insert - it gives an error for create or update.</p>
</blockquote>

<p>in <a href="http://www.django-rest-framework.org/api-guide/serializers/#saving-instances" rel="nofollow">Django rest_framework's documentation</a> (link provided by @Abdulafaja) it give only one example for a nested serializer, and it's a one to many relationship (Album->tracks) but mine is one to one (book->author) so I have no idea how to serilize the nested feature. This API needs to give the frontend all CRUD features.</p>

<p>Thanks.</p>

<blockquote>
  <p><strong>EDITED serializers.py by Abdullah:</strong></p>
</blockquote>

<pre><code>from authors.models import Book, Author
from rest_framework import serializers


class AuthorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Author
        book_author = 'name'


class BookSerializer(serializers.ModelSerializer):
    author = AuthorSerializer(many=False)

    class Meta:
        model = Book
        fields = ('name', 'author')

        def create(self, validated_data):
            author, _ = Author.objects.get_or_create(name=validated_data.get('author').get('name'))
            return Book.objects.create(name=validated_data.get('name'), author=author)

        def update(self, instance, validated_data):
            author = validated_data.get('author')
            if author:
                instance.author = Author.objects.get_or_create(name=author.get('name'))
            instance.name = validated_data.get('name', instance.name)
            instance.save()
            return instance
</code></pre>

<p>For Book model Author is just an id of that field in database, so that's why it's return an integer field.
Try to add an Author field in Book serializer</p>

<pre><code>class BookSerializer(serializers.ModelSerializer):
    author = AuthorSerializer(read_only=True)

    class Meta:
        model = Book
        fields = ('name', 'author')
</code></pre>

<p>This is a NestedSerializer, which is read-only by default. On it's DRF doc site <a href="http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers" rel="nofollow">link</a> is mentioned that you </p>

<blockquote>
  <p>need to create create() and/or update() methods in order to explicitly specify how the child relationships should be saved.</p>
</blockquote>

<p>So your BookSerializer needs to look like this</p>

<pre><code>class BookSerializer(serializers.ModelSerializer):
    author = AuthorSerializer(read_only=True)

    class Meta:
        model = Book
        fields = ('name', 'author')

    def create(self, validated_data):
        # Create new object

    def update(self, instance, validated_data):
        # Update existing instance
</code></pre>

<p>Both <code>create</code> and <code>update</code> methods need to save the object to the database and return it. There are called when you save your BookSerializer class instance.
The difference between them is that <code>create</code> is called when you create new instance of Book object </p>

<pre><code>serializer = BookSerializer(data=data)
</code></pre>

<p>and <code>update</code> is called if you passed an existing instance of Book object when instantiating the serializer class  </p>

<pre><code>serializer = BookSerializer(book, data=data)
</code></pre>

<p>More information you can find <a href="http://www.django-rest-framework.org/api-guide/serializers/#saving-instances" rel="nofollow">here</a></p>

<p><strong>EDIT:</strong>
If you want to create an instance with NestedSerializer it should not be read only field.</p>

<pre><code>class BookSerializer(serializers.ModelSerializer):
    author = AuthorSerializer(many=False)

    class Meta:
        model = Book
        fields = ('name', 'author')

    def create(self, validated_data):
        author, _ = Author.objects.get_or_create(name=validated_data.get('author').get('name'))
        return Book.objects.create(name=validated_data.get('name'), author=author)

    def update(self, instance, validated_data):
        author = validated_data.get('author')
        if author:
            instance.author = Author.objects.get_or_create(name=author.get('name'))
        instance.name = validated_data.get('name', instance.name)
        instance.save()
        return instance
</code></pre>

python django django-rest-framework