Como se faz pra retornar a consulta do banco, como JSON ?
Você está vendo a versão anterior da nova experiência da Alura que estamos preparando para você. Em breve, ela ganha uma identidade visual novinha totalmente pensada em potencializar seus estudos!
Como se faz pra retornar a consulta do banco, como JSON ?
Oi Thiago, tudo bem ?
Você consegue fazer algo assim :
from django.http import JsonResponse
return JsonResponse({'foo':'bar'})
Assim correto ?
from django.shortcuts import render, redirect
from perfis.models import Perfil
from django.http import JsonResponse
# Create your views here.
def index(request):
perfis = Perfil.objects.all()
return JsonResponse({'perfis':perfis})
Tá dando o seguinte error:
TypeError at /
[<Perfil: Perfil object>, <Perfil: Perfil object>, <Perfil: Perfil object>] is not JSON serializable
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.7.4
Exception Type: TypeError
Exception Value:
[<Perfil: Perfil object>, <Perfil: Perfil object>, <Perfil: Perfil object>] is not JSON serializable
Exception Location: /usr/lib/python2.7/json/encoder.py in default, line 184
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/thiagod/Projetos/Python/Django/connectedin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Server time: Fri, 9 Feb 2018 15:08:37 +0000
Acho que pode fazer
def index(request):
perfis = Perfil.objects.all()
return JsonResponse({perfis})
from django.shortcuts import render, redirect
from perfis.models import Perfil
from django.http import JsonResponse
# Create your views here.
def index(request):
perfis = Perfil.objects.all()
return JsonResponse({perfis})
TypeError at /
In order to allow non-dict objects to be serialized set the safe parameter to False
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.7.4
Exception Type: TypeError
Exception Value:
In order to allow non-dict objects to be serialized set the safe parameter to False
Exception Location: /usr/local/lib/python2.7/dist-packages/django/http/response.py in __init__, line 494
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/thiagod/Projetos/Python/Django/connectedin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Server time: Fri, 9 Feb 2018 15:27:03 +0000
Olá Thiago, acho que você pode fazer dessa maneira
from django.shortcuts import render, redirect
from perfis.models import Perfil
from django.http import JsonResponse
# Create your views here.
def index(request):
perfis = Perfil.objects.all()
return JsonResponse(perfis, safe=False)
Ainda tá
TypeError at /
[<Perfil: Perfil object>, <Perfil: Perfil object>, <Perfil: Perfil object>] is not JSON serializable
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.7.4
Exception Type: TypeError
Exception Value:
[<Perfil: Perfil object>, <Perfil: Perfil object>, <Perfil: Perfil object>] is not JSON serializable
Exception Location: /usr/lib/python2.7/json/encoder.py in default, line 184
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/thiagod/Projetos/Python/Django/connectedin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Server time: Thu, 15 Feb 2018 13:09:29 +0000
Oi Thiago, tudo bem?
Acho que você consegue utilizar o módulo django.core.serializers
from django.shortcuts import render, redirect
from perfis.models import Perfil
from django.core import serializers
# Create your views here.
def index(request):
perfis = Perfil.objects.all()
return serializers.serialize("json", perfis)
Tudo bem Yuri, e contigo ?
AttributeError at /
'unicode' object has no attribute 'get'
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.7.4
Exception Type: AttributeError
Exception Value:
'unicode' object has no attribute 'get'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/middleware/clickjacking.py in process_response, line 31
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/home/thiagod/Projetos/Python/Django/connectedin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Server time: Thu, 15 Feb 2018 16:36:56 +0000