Сериализаторы Django не могут сериализовать набор запросов в JSON
Пробую простой сериализатор Django. Я использую queryset
, с определенными значениями. Затем я передаю сериализатору queryset
или queryset.values()
. Почему он выдает ошибку 500?
@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).values_list("geoAsciiName", "geoLongitude", "geoLatitude", "geo_timezone", "geo_population", "geoCountry_code").order_by('-geo_population')[:5]
data = serializers.serialize("json", queryset.values()) # tried queryset and queryset.values()
parsed_data = json.loads(data)
pretty_json = json.dumps(parsed_data, indent=4)
return JsonResponse(json.loads(pretty_json), safe=False)
[24/Jul/2024 06:07:52] "GET /edit_chart_form HTTP/1.1" 200 18929
Internal Server Error: /geolookup/US/p
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\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\My Drive\myProgram\runtime\devui\ui\views.py", line 81, in geoLookup
data = serializers.serialize("json", queryset.values())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\serializers\__init__.py", line 134, in serialize
s.serialize(queryset, **options)
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\serializers\base.py", line 113, in serialize
concrete_model = obj._meta.concrete_model
^^^^^^^^^
AttributeError: 'dict' object has no attribute '_meta'
[24/Jul/2024 06:07:55] "GET /geolookup/US/p HTTP/1.1" 500 105779