<bdo id='ZYiq3'></bdo><ul id='ZYiq3'></ul>

      <small id='ZYiq3'></small><noframes id='ZYiq3'>

    1. <tfoot id='ZYiq3'></tfoot>
    2. <i id='ZYiq3'><tr id='ZYiq3'><dt id='ZYiq3'><q id='ZYiq3'><span id='ZYiq3'><b id='ZYiq3'><form id='ZYiq3'><ins id='ZYiq3'></ins><ul id='ZYiq3'></ul><sub id='ZYiq3'></sub></form><legend id='ZYiq3'></legend><bdo id='ZYiq3'><pre id='ZYiq3'><center id='ZYiq3'></center></pre></bdo></b><th id='ZYiq3'></th></span></q></dt></tr></i><div id='ZYiq3'><tfoot id='ZYiq3'></tfoot><dl id='ZYiq3'><fieldset id='ZYiq3'></fieldset></dl></div>

      1. <legend id='ZYiq3'><style id='ZYiq3'><dir id='ZYiq3'><q id='ZYiq3'></q></dir></style></legend>
      2. &lt;Django 对象&gt;不是 JSON 可序列化的

        时间:2024-04-21
        • <legend id='VxUiC'><style id='VxUiC'><dir id='VxUiC'><q id='VxUiC'></q></dir></style></legend>

          <small id='VxUiC'></small><noframes id='VxUiC'>

            <bdo id='VxUiC'></bdo><ul id='VxUiC'></ul>
          • <i id='VxUiC'><tr id='VxUiC'><dt id='VxUiC'><q id='VxUiC'><span id='VxUiC'><b id='VxUiC'><form id='VxUiC'><ins id='VxUiC'></ins><ul id='VxUiC'></ul><sub id='VxUiC'></sub></form><legend id='VxUiC'></legend><bdo id='VxUiC'><pre id='VxUiC'><center id='VxUiC'></center></pre></bdo></b><th id='VxUiC'></th></span></q></dt></tr></i><div id='VxUiC'><tfoot id='VxUiC'></tfoot><dl id='VxUiC'><fieldset id='VxUiC'></fieldset></dl></div>

              1. <tfoot id='VxUiC'></tfoot>

                    <tbody id='VxUiC'></tbody>

                  本文介绍了&lt;Django 对象&gt;不是 JSON 可序列化的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have the following code for serializing the queryset;

                  def render_to_response(self, context, **response_kwargs):
                  
                      return HttpResponse(json.simplejson.dumps(list(self.get_queryset())),
                                          mimetype="application/json")
                  

                  And following is my get_querset()

                  [{'product': <Product: hederello ()>, u'_id': u'9802', u'_source': {u'code': u'23981', u'facilities': [{u'facility': {u'name': {u'fr': u'Gxe9nxe9ral', u'en': u'General'}, u'value': {u'fr': [u'bar', u'rxe9ception ouverte 24h/24', u'chambres non-fumeurs', u'chambres familiales',.........]}]
                  

                  Which I need to serialize. But it says not able to serialize the <Product: hederello ()>. Because list composed of both django objects and dicts. Any ideas ?

                  解决方案

                  simplejson and json don't work with django objects well.

                  Django's built-in serializers can only serialize querysets filled with django objects:

                  data = serializers.serialize('json', self.get_queryset())
                  return HttpResponse(data, content_type="application/json")
                  

                  In your case, self.get_queryset() contains a mix of django objects and dicts inside.

                  One option is to get rid of model instances in the self.get_queryset() and replace them with dicts using model_to_dict:

                  from django.forms.models import model_to_dict
                  
                  data = self.get_queryset()
                  
                  for item in data:
                     item['product'] = model_to_dict(item['product'])
                  
                  return HttpResponse(json.simplejson.dumps(data), mimetype="application/json")
                  

                  Hope that helps.

                  这篇关于&lt;Django 对象&gt;不是 JSON 可序列化的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django-注册&amp;Django-Profile,使用您自己的自定义表单 下一篇:最喜欢的 Django Tips &amp;特征?

                  相关文章

                    <i id='TiPOI'><tr id='TiPOI'><dt id='TiPOI'><q id='TiPOI'><span id='TiPOI'><b id='TiPOI'><form id='TiPOI'><ins id='TiPOI'></ins><ul id='TiPOI'></ul><sub id='TiPOI'></sub></form><legend id='TiPOI'></legend><bdo id='TiPOI'><pre id='TiPOI'><center id='TiPOI'></center></pre></bdo></b><th id='TiPOI'></th></span></q></dt></tr></i><div id='TiPOI'><tfoot id='TiPOI'></tfoot><dl id='TiPOI'><fieldset id='TiPOI'></fieldset></dl></div>

                      <tfoot id='TiPOI'></tfoot>
                      <legend id='TiPOI'><style id='TiPOI'><dir id='TiPOI'><q id='TiPOI'></q></dir></style></legend>
                      • <bdo id='TiPOI'></bdo><ul id='TiPOI'></ul>

                      <small id='TiPOI'></small><noframes id='TiPOI'>