ここで問題が発生しています。メッセージとモデル インスタンスで構成される JSON 応答を返そうとしています。
class MachineModel(models.Model):
name = models.CharField(max_length=64, blank=False)
description = models.CharField(max_length=64, blank=False)
manufacturer = models.ForeignKey(Manufacturer)
added_by = models.ForeignKey(User, related_name='%(app_label)s_%(class)s_added_by')
creation_date = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
machine_model_model = form.save(commit=False)
r_user = request.user.userprofile
machine_model_model.manufacturer_id = manuf_id
machine_model_model.added_by_id = request.user.id
machine_model_model.save()
alert_message = " The'%s' model " % machine_model_model.name
alert_message += ("for '%s' " % machine_model_model.manufacturer)
alert_message += "was was successfully created!"
test = simplejson.dumps(list(machine_model_model))
data = [{'message': alert_message, 'model': test}]
response = JSONResponse(data, {}, 'application/json')
class JSONResponse(HttpResponse):
"""JSON response class."""
def __init__(self, obj='', json_opts={}, mimetype="application/json", *args, **kwargs):
content = simplejson.dumps(obj, **json_opts)
super(JSONResponse,self).__init__(content, mimetype, *args, **kwargs)
しかし、私は取得し続けます:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <MachineModel: "Test12"> is not JSON serializable
何故ですか?私は前に見た:
models = Model.objects.filter(manufacturer_id=m_id)
json = simplejson.dumps(models)
それは機能します...違いは何ですか?!
ありがとう!