django의serializer 서열화 모델

1411 단어 날생선
Object of type ImageFieldFile is not JSON serializable
실현 1
from django.views.generic.base import View
from django.http import HttpResponse
from goods.models import Goods
from django.core import serializers

class GoodsListView(View):
    def get(self, request):
        '''
          django view       
        :param request:
        :return:
        '''
        goods = Goods.objects.all()[:10]

        json_data = serializers.serialize('json', goods)
        return HttpResponse(json_data, content_type='application/json')


실현
from django.views.generic.base import View
from django.http import JsonResponse
from goods.models import Goods
from django.core import serializers
import json

class GoodsListView(View):
    def get(self, request):
        '''
          django view       
        :param request:
        :return:
        '''
        goods = Goods.objects.all()[:10]

        json_data = serializers.serialize('json', goods)
        json_data = json.loads(json_data)
        return JsonResponse(json_data, safe=False)


part2 올드보이 뎅고 서열화
  • serialize는queryset만 서열화
  • list()
  •     user_list = UserInfo.objects.values('email')
        user_list = list(user_list) # queryset  list
        return HttpResponse(dumps(user_list))
    

    좋은 웹페이지 즐겨찾기