1๐
โโ- comparison of HTTP
request โโ-
-
GET
โ simply retrieve data from the database (no need older data) -
POST
โ simply insert data in a database no (no need older data) -
PUT
โ simply update data record (need older data record + new data from request BODY) -
PATCH
โ simply update data record (need older data record + new data from request BODY) (partially update)
โโโ Example (Where we can use instance and data ) โโโโ
class StudentAPI(APIView):
def get(self, request, pk=None, format=None):
id = pk
if id is not None:
stu = Student.objects.get(id=id)
serializer = StudentSerializer(stu)
return Response(serializer.data)
stu = Student.objects.all()
serializer = StudentSerializer(stu, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = StudentSerializer(data=request.data) # here no need any instance for insert data
if serializer.is_valid():
serializer.save()
return Response({'msg':'Data Created'}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# Complete Data Updated (all fields need)
def put(self, request, pk, format=None):
id = pk
stu = Student.objects.get(pk=id) # this is instance
serializer = StudentSerializer(stu, data=request.data) # here need instance for update data
if serializer.is_valid():
serializer.save()
return Response({'msg':'Complete Data Updated'})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# Complete Data Updated (Not all fields need)
def patch(self, request, pk, format=None):
id = pk
stu = Student.objects.get(pk=id) # this is instance
serializer = StudentSerializer(stu, data=request.data, partial=True) # also here need instance for update data
if serializer.is_valid():
serializer.save()
return Response({'msg':'Partial Data Updated'})
return Response(serializer.errors)
so, your serializers
look like this (which you passed in your question)
serializer = ModelSerializer(data=request.data)
serializer = ModelSerializer(instance=queryset, data=request.data)
conclusion:
-
instance
means a single record of the data table, that can pass at updation time. -
data
means a request
the body can pass at updation time and insertion time both.
0๐
In ModelSerializer
we can pass an object of a model or queryset (multiple objects of a model) with many=True
in the instance parameter. On the other hand, the data parameter takes data to run validations on it and saves it on DB. Click here for more info
- [Answered ]-Queryset Cannot reorder a query once a slice has been taken
- [Answered ]-Need helping with date format in Django
- [Answered ]-Django simple debugging gives strange result
- [Answered ]-How to redirect to another page
0๐
Nothing much , Just remember that things
1] mostly data are that data which are not stored in the data base or (means you want to POST(insert) that data into DB or maybe you want to PUT or PATCH (update) that data into db,
2] instance means, you are giving an instance to DB, which data (which queryset) you want to update.
class SnippetList(APIView):
"""
List all snippets, or create a new snippet.
"""
def get(self, request, format=None):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
above example of data = queryset means (POST)
class SnippetDetail(APIView):
"""
Retrieve, update or delete a snippet instance.
"""
def get_object(self, pk):
try:
return Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet)
return Response(serializer.data)
def put(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(instance = snippet, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
above example of instace means (PUT or PATCH)
for more information i suggest you to explore click here to view this website
Thank you .๐๐ป