tarp20
tarp20

Reputation: 695

How to efficiently parse multiple data with json?

I have Django REST framework app

When for example I make POST book with title Hobbit

I want to fetch data into database but problem

https://www.googleapis.com/books/v1/volumes?q=Hobbit

I got many many book with this title and I only need specific data like:

my models:

class Book(models.Model):
    title = models.CharField(
        max_length=249,
        null=True)
    autors = models.CharField(
        max_length=500,
        null=True)
    published_date = models.CharField(
        max_length=100,
        blank=True)
    categories = models.CharField(
        max_length=500,
        null=True)
    average_rating = models.PositiveBigIntegerField(
        blank=True)
    ratings_count = models.PositiveBigIntegerField(
        null=True)
    thumbnail = models.CharField(
        max_length=300,
        null=True)

What best practise to parse all this data and save into database ?

I already know how with requests get to the data I need.

Problem is how effectively use drf and implement this feature in the best way .

gp

Upvotes: 0

Views: 197

Answers (1)

ThomasGth
ThomasGth

Reputation: 870

As the names and types in the response are not exactly the same than in your model, you will need to do a little bit of custom mapping.
Then you have two choices to create your objects, either calling Book.objects.create() or serializer.save()

Most of the time, you will just want to stick to serializers.

import requests
import json
from book.models import Book
from book.serializers import BookSerializer

items = json.loads(requests.get("https://www.googleapis.com/books/v1/volumes?q=Hobbit").content).get('items')

for book in items:
    mapping = {
        'title': book['volumeInfo']['title'],
        'autors': ','.join(book['volumeInfo']['authors']),
        ...
    }

    # Via a serializer
    serializer = BookSerializer(**mapping)
    if serializer.is_valid():
        instance = serializer.save()

    # Via Django ORM
    instance = Book.objects.create(**mapping)
    

You can use a ModelSerializer (see here), that would look like this :

from rest_framework import serializers

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ['title', 'autors', 'published_date', ...] # or '__all__' if you want them all

Upvotes: 3

Related Questions