Cannot exclude pk and model from serializer in django 1.8

Question!

I'm trying to get rid of pk and model from the output json object as part of Django serialization. I tried the below code in views.py, but it threw the below error

TypeError: serialize() takes 2 positional arguments but 3 were given.

To solve this, I used @staticmethod as well, but no much use.

Here is the views.py

def get_stats(request):
    if request.method == "POST":
        srch_dropV = request.POST['srch_dropAJ']
    else:
        srch_dropV = ''
    if(srch_dropV == 'Lock'):
        stud = LockBased.objects.all()
    response_data = {}
    response_data['result'] = 'Success'
    serializer = NewSerializer()
    response_data['message'] = serializer.serialize('json', stud)
    return HttpResponse(JsonResponse(response_data), content_type="application/json")


from django.core.serializers.json import Serializer, DjangoJSONEncoder
import json

class NewSerializer(Serializer):
    def end_serialization(self):
        cleaned_objects = []

        for obj in self.objects:
            del obj['pk']
            del obj['model'] 
            cleaned_objects.append(obj)

        json.dump(cleaned_objects, self.stream, cls=DjangoJSONEncoder, **self.options)

Please help me to identify the issue and fix it.
I'm using Django 1.8 and Python 3.4.



Answers

I think you confused serializers.serialize() with serializer.serialize():

serializer = NewSerializer()
response_data['message'] = serializer.serialize('json', stud)

In contrast to the module-level function, the serializer's method does not take a target format argument, but only a queryset:

response_data['message'] = serializer.serialize(stud)
By : dhke


This video can help you solving your question :)
By: admin