<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 可序列化的

        lt;Django object gt; is not JSON serializable(lt;Django 对象gt;不是 JSON 可序列化的)
        • <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 可序列化的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                      <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>
                          <tbody id='TiPOI'></tbody>
                          <bdo id='TiPOI'></bdo><ul id='TiPOI'></ul>

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