What are the differences between using serializers and for loop to return a json response?

Viewed 371

I am trying to create an API to return some JSON responses with Django. The app is just doing some CRUD operations and integration with Shopify API. From my understanding, I know that I can use Django Rest Framework (DRF), plain Django serializer, or return JSON response on my own.

From this thread, I know that DRF can give me a better JSON format than Django serializer.

However, is it simpler to just loop the Model and return the JSON Response on my own? For example,

for product in products:
    if 'node' in product:
        returnProduct = {}
        node = product['node']
        returnProduct['id'] = node['id'][22:]
        returnProduct['updated_at'] = node['updatedAt'].split('T', 1)[0]
        returnProduct['title'] = node['title']
        returnProduct['short_title'] = node['title'][0:48] + "..." if len(node['title']) > 48 else node['title']
        returnProducts.append(returnProduct)

data = { "products" : returnProducts}

return JsonResponse(data)

Is it because it's faster to use serializers than looping on my own? Or am I mixing up different scenarios?

0 Answers
Related