Формат JSON из сериализатора DjangoPython

Программы на Python
Anonymous
Формат JSON из сериализатора Django

Сообщение Anonymous »

Я использую сериализатор Django для JSON.
Использую документацию здесь. https://docs.djangoproject.com/en/5.0/t ... alization/
Почему сериализатор выдает неверный вывод? Документация не предлагает решения.
Как получить чистый, хорошо отформатированный JSON без pk и модели?
Просмотреть
from django.http import HttpResponse, JsonResponse
from django.core import serializers

@csrf_protect
def cityLookup(request, **kwargs):
country = kwargs.get('Country')
city = kwargs.get('Place')
queryset = GeoNames_location.objects.filter(geoCountry_code=country, feature_class='P', geoAsciiName__istartswith=city).order_by('-geo_population')[:5]
data = serializers.serialize("json", queryset, fields=["geoAsciiName", "geoLongitude", "geoLatitude", "geo_timezone", "geo_population", "geoCountry_code"])
return JsonResponse(data, safe=False) # Need security check

Ответ
"[{\"model\": \"ui.geonames_location\", \"pk\": 5128581, \"fields\": {\"geoAsciiName\": \"New York City\", \"geoLatitude\": \"40.714270\", \"geoLongitude\": \"-74.005970\", \"geoCountry_code\": \"US\", \"geo_population\": 8804190, \"geo_timezone\": \"America/New_York\"}}, {\"model\": \"ui.geonames_location\", \"pk\": 4645421, \"fields\": {\"geoAsciiName\": \"New South Memphis\", \"geoLatitude\": \"35.086760\", \"geoLongitude\": \"-90.056760\", \"geoCountry_code\": \"US\", \"geo_population\": 641608, \"geo_timezone\": \"America/Chicago\"}}, {\"model\": \"ui.geonames_location\", \"pk\": 4335045, \"fields\": {\"geoAsciiName\": \"New Orleans\", \"geoLatitude\": \"29.954650\", \"geoLongitude\": \"-90.075070\", \"geoCountry_code\": \"US\", \"geo_population\": 389617, \"geo_timezone\": \"America/Chicago\"}}, {\"model\": \"ui.geonames_location\", \"pk\": 5101798, \"fields\": {\"geoAsciiName\": \"Newark\", \"geoLatitude\": \"40.735660\", \"geoLongitude\": \"-74.172370\", \"geoCountry_code\": \"US\", \"geo_population\": 281944, \"geo_timezone\": \"America/New_York\"}}, {\"model\": \"ui.geonames_location\", \"pk\": 4776024, \"fields\": {\"geoAsciiName\": \"Newport News\", \"geoLatitude\": \"36.980380\", \"geoLongitude\": \"-76.429750\", \"geoCountry_code\": \"US\", \"geo_population\": 182385, \"geo_timezone\": \"America/New_York\"}}]"

После удаления JsonResponse
@csrf_protect
def geoLookup(request, **kwargs):
country = kwargs.get('Country')
city = kwargs.get('Place')
queryset =
GeoNames_location.objects.filter(geoCountry_code=country, feature_class='P', geoAsciiName__istartswith=city).order_by('-geo_population')[:5]
data = serializers.serialize("json", queryset, fields=["geoAsciiName", "geoLongitude", "geoLatitude", "geo_timezone", "geo_population", "geoCountry_code"])
return (data)

Internal Server Error: /geolookup/US/new
Traceback (most recent call last):
File
"C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File
"C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\Django\utils\deprecation.py", line 136, in __call__
response = self.process_response(request, response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File
"C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response
if response.get("X-Frame-Options") is not None:
^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'
[23/Jul/2024 15:32:43] "GET /geolookup/US/new HTTP/1.1" 500 72755


Подробнее здесь: https://stackoverflow.com/questions/787 ... serializer

Вернуться в «Python»