简体   繁体   中英

Django JSON serializable error

With the following code below, There is an error saying

      File "/home/user/web_pro/info/views.py", line 184, in headerview,
      raise TypeError("%r is not JSON serializable" % (o,))
      TypeError: <lastname: jerry> is not JSON serializable

In the models code

  header(models.Model):
   firstname = models.ForeignKey(Firstname)
   lastname = models.ForeignKey(Lastname)

In the views code

  headerview(request):
       header = header.objects.filter(created_by=my_id).order_by(order_by)[offset:limit]


      l_array = []
      l_array_obj = []
      for obj in header:

           l_array_obj = [obj.title, obj.lastname ,obj.firstname ]
           l_array.append(l_array_obj)
      dictionary_l.update({'Data': l_array}) ; 
      return HttpResponse(simplejson.dumps(dictionary_l), mimetype='application/javascript')

what is this error and how to resolve this?

thanks..

The quick read is that obj.lastname is a Lastname model not a String. You probably need to say something like:

l_array_obj = [..., obj.lastname.value, .... ]

to get the string value, rather than the Model object.

您是否考虑过使用Django 自己的序列化功能

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM