74

With django-rest-framework 3.0 and having these simple models:

class Book(models.Model):
    title = models.CharField(max_length=50)


class Page(models.Model):
    book = models.ForeignKey(Books, related_name='related_book')
    text = models.CharField(max_length=500)

And given this JSON request:

{
   "book_id":1,
   "pages":[
      {
         "page_id":2,
         "text":"loremipsum"
      },
      {
         "page_id":4,
         "text":"loremipsum"
      }
   ]
}

How can I write a nested serializer to process this JSON and for each page for the given book either create a new page or update if it exists.

class RequestSerializer(serializers.Serializer):
    book_id = serializers.IntegerField()
    page = PageSerializer(many=True)


class PageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page

I know that instantiating the serializer with an instance will update the current one but how should I use it inside the create method of nested serializer?

Sam R.
  • 16,027
  • 12
  • 69
  • 122

2 Answers2

109

Firstly, do you want to support creating new book instances, or only updating existing ones?

If you only ever wanted to create new book instances you could do something like this...

class PageSerializer(serializers.Serializer):
    text = serializers.CharField(max_length=500)

class BookSerializer(serializers.Serializer):
    page = PageSerializer(many=True)
    title = serializers.CharField(max_length=50)

    def create(self, validated_data):
        # Create the book instance
        book = Book.objects.create(title=validated_data['title'])

        # Create or update each page instance
        for item in validated_data['pages']:
            page = Page(id=item['page_id'], text=item['text'], book=book)
            page.save()

        return book

Note that I haven't included the book_id here. When we're creating book instances we won't be including a book id. When we're updating book instances we'll typically include the book id as part of the URL, rather than in the request data.

If you want to support both create and update of book instances then you need to think about how you want to handle pages that are not included in the request, but are currently associated with the book instance.

You might choose to silently ignore those pages and leave them as they are, you might want to raise a validation error, or you might want to delete them.

Let's assume that you want to delete any pages not included in the request.

def create(self, validated_data):
    # As before.
    ...

def update(self, instance, validated_data):
    # Update the book instance
    instance.title = validated_data['title']
    instance.save()

    # Delete any pages not included in the request
    page_ids = [item['page_id'] for item in validated_data['pages']]
    for page in instance.books:
        if page.id not in page_ids:
            page.delete()

    # Create or update page instances that are in the request
    for item in validated_data['pages']:
        page = Page(id=item['page_id'], text=item['text'], book=instance)
        page.save()

    return instance

It's also possible that you might want to only support book updates, and not support creation, in which case, only include the update() method.

There are also various ways you could reduce the number of queries eg. using bulk create/deletion, but the above would do the job in a fairly straightforward way.

As you can see there are subtleties in the types of behavior you might want when dealing with nested data, so think carefully about exactly what behavior you're expecting in various cases.

Also note that I've been using Serializer in the above example rather than ModelSerializer. In this case it's simpler just to include all the fields in the serializer class explicitly, rather than relying on the automatic set of fields that ModelSerializer generates by default.

Tom Christie
  • 33,394
  • 7
  • 101
  • 86
  • 1
    `you might want to only support book updates ... , only include the update() method`. In this case, how the `instance` in update method will be filled with an existing book? – Sam R. Dec 20 '14 at 23:31
  • By instantiating the serialiser with the 'instance' keyword argument. Normally you'd get that by performing a lookup based on a primary key in the URL. If you're using the generic views that'd be handled for you. Take a look at DetailMixin in 'mixins.py' for the implementation of that. – Tom Christie Dec 22 '14 at 23:33
  • 1
    Thanks Tom. I got it now. – Sam R. Dec 23 '14 at 04:54
  • 2
    Why the override is in the serializer and not in the view, like : http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing ? – CheapD AKA Ju Jan 08 '15 at 07:41
  • 3
    @TomChristie Could you do me the favor and have a look at my [attempt to create nested resources](http://stackoverflow.com/questions/29126707/improperly-configured-nested-resource-using-hyperlinkedmodelserializer)? I am out of ideas after trying *[django-rest-framework-nested-resource](https://github.com/simpleenergy/django-rest-framework-nested-resource)*, *[drf-extensions](https://github.com/chibisov/drf-extensions)* and *[drf-nested-routers](https://github.com/alanjds/drf-nested-routers)* - all without success. I am happy to switch to something that actually works. – JJD Mar 19 '15 at 16:46
  • @TomChristie rest-v3.2.5 is failing when I try to do `for page in instance.books`. The error says RelatedManager is not iterable. Is there a fix for this in a newer version? I used pip to install rest, so I feel like I'm using a pretty recent version. – Seaux Sep 25 '15 at 02:35
  • 1
    @Seaux try `for page in instance.books.all()` instead. Or even better, replace `all` with `iterator` – alfetopito Nov 12 '15 at 13:36
  • 2
    @TomChristie If I use ModelSerializer instead of Serializer it filters out `page_id`. – Sassan Nov 12 '16 at 10:19
  • @Sassan Just add an 'id' field in the related field serializer. In the OP's question , in the PageSerializer use "fields = ('id',...) in calss meta. – Ryu_hayabusa Dec 23 '16 at 05:35
  • @Sassan also `id = serializers.IntegerField()` – Ryu_hayabusa Dec 23 '16 at 05:42
  • This line `instance.title = validated_data['title']` is still needed? The .save() by it self will get all fields from validated_data, right? – Dilvane Zanardine Mar 03 '17 at 20:18
  • @TomChristie What if there is a `validate_` on the `PageSerializer`? With this approach validation won't be done on the Page object being created. Any Help? – Sreekanth Reddy Balne Sep 16 '18 at 02:26
  • If you have a lot of attributes to update in `Book` a nice way to do it is to do `validated_data.pop("pages")` and deal with the pages yourself but let DRF deal with the `Book` by calling `super().update(instance, validated_data)`. – Agustín Lado Apr 12 '20 at 18:40
  • `page_ids = [item['page_id'] for item in validated_data['pages']]` this fails if one of the items is new and lacks an id. `page_ids = [item['page_id'] for item in validated_data['pages'] if 'page_id' in item]` works around that. – Paul Schreiber Oct 07 '20 at 20:11
3

You can simply use drf-writable-nested. It automatically make your nested serializers writable and updatable.

in you serializers.py:

from drf_writable_nested import WritableNestedModelSerializer

class RequestSerializer(WritableNestedModelSerializer):
    book_id = serializers.IntegerField()
    page = PageSerializer(many=True)


class PageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page

And that's it!

Also the library supports using only one of the create and update logics if you don't need both.

Peter Sobhi
  • 2,542
  • 2
  • 22
  • 28