In django, I was able to pass data using dictionary. Like I set the objects in my dictionary and pass it in return render and call the object in frontend (return render(request, 'c.html', context) right? so How can I do this in django rest?
2 Answers
You may return Response in rest framework like this if you are using django rest framework.
context = {'key':'value'}
return Response(context)
Or if you are using a serializer then
return Response(serializer.data)

- 690
- 6
- 9
-
So how can I pass two serializer at the same time in one view? Suppose I have two serializer, one is product serializer which contain all products and another serializer for related products, in the individual product page i want to show product info and related products so now how am I gonna pass related product info? – Sohanur Rahman Shanto 16209570 Aug 28 '21 at 06:34
-
You can create context with both the serializers data. `context = { 'product':product_serializer.data, 'related_products': related_products_serializer.data } return Response(context)` – Ashin Shakya Aug 28 '21 at 06:59
In Django REST Framework the concept of Serializing is to convert DB data to a datatype that can be used by javascript. Every serializer comes with some field that is going to be processed. For example, if you have a class with the name Employee and its fields as Employee_id, Employee_name, is_admin, etc. Then, you would need AutoField, CharField, and BooleanField for storing and manipulating data through Django. Similarly, serializer also works with the same principle and has fields that are used to create a serializer.
DictField is basically a dictionary field that validates the input against a dictionary of objects. It has the following arguments:
child and allow_empty like this>>>
field_name = serializers.DictField(*args, **kwargs)
for example document = DictField(child=CharField())
you can use serializer like below>>>
from rest_framework import serializer
class Any(object):
def __init__(self, dictonary):
self.dict = dictionary
class AnySerializer(serializers.Serializer):
dictionary = serializers.DictField(
child = serializers.CharField())
you can visit similar problem for understanding through the real problem.
And this link is the complete documentation of your problem. You can check this out.

- 339
- 1
- 3
- 11
-
So how can I pass two serializer at the same time in one view? Suppose I have two serializer, one is product serializer which contain all products and another serializer for related products, in the individual product page i want to show product info and related products so now how am I gonna pass related product info? – Sohanur Rahman Shanto 16209570 Aug 28 '21 at 06:34
-