Django models are not ajax serializable

丶灬走出姿态 提交于 2019-12-03 07:34:52

问题


I have a simple view that I'm using to experiment with AJAX.

def get_shifts_for_day(request,year,month,day):

    data= dict()
    data['d'] =year
    data['e'] = month
    data['x'] = User.objects.all()[2]

    return HttpResponse(simplejson.dumps(data), mimetype='application/javascript')

This returns the following:

TypeError at /sched/shifts/2009/11/9/

<User: someguy> is not JSON serializable

If I take out the data['x'] line so that I'm not referencing any models it works and returns this:

{"e": "11", "d": "2009"}

Why can't simplejson parse my one of the default django models? I get the same behavior with any model I use.


回答1:


You just need to add, in your .dumps call, a default=encode_myway argument to let simplejson know what to do when you pass it data whose types it does not know -- the answer to your "why" question is of course that you haven't told poor simplejson what to DO with one of your models' instances.

And of course you need to write encode_myway to provide JSON-encodable data, e.g.:

def encode_myway(obj):
  if isinstance(obj, User):
    return [obj.username,
            obj.firstname,
            obj.lastname,
            obj.email]
    # and/or whatever else
  elif isinstance(obj, OtherModel):
    return [] # whatever
  elif ...
  else:
    raise TypeError(repr(obj) + " is not JSON serializable")

Basically, JSON knows about VERY elementary data types (strings, ints and floats, grouped into dicts and lists) -- it's YOUR responsibility as an application programmer to match everything else into/from such elementary data types, and in simplejson that's typically done through a function passed to default= at dump or dumps time.

Alternatively, you can use the json serializer that's part of Django, see the docs.



来源:https://stackoverflow.com/questions/1457735/django-models-are-not-ajax-serializable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!