I get the data from the table in the following way:

def get_users(request): users = User.objects.all() data = serializers.serialize('json', users) return HttpResponse(data, content_type='application/json') 

And in response, I get the following:

 {"model": "api.user", "pk": 1, "fields": {"first_name": "Vlad", "second_name": "Sapozhnikov", "age": 21}} 

I don't understand where the model, pk and fields fields come from and is there any way to get json without them? Thanks in advance for your reply, I am new to Django.

  • Comments are not intended for extended discussion; conversation moved to chat . - Yuriy SPb

1 answer 1

This is how the standard Django serializer works. If you need to get a "clean" json, you can write this:

 import json from django.forms.models import model_to_dict from django.http import HttpResponse from main.models import User def get_users(request): users = User.objects.all() data = json.dumps([model_to_dict(obj) for obj in users]) return HttpResponse(data, content_type='application/json') 
  • I have already found a solution, but still thank you for the answer! - Vlad Sapozhnikov