开发者

django jquery json deserialize

I have models for eg like this

class Model():
time=models.DateTimeField()
free=models.BooleanField(default=Fals开发者_如何学运维e)
user=models.ForeignKey(User) //it is my own User class
full_info=models.TextField()

than I serialize it:

data=serializers.serialize("json",Model.objects.all())
        return HttpResponse(data,mimetype='application/json')

and here is my jquery code I use to fetch it:

$.post("/url",data,
                    function(data){


                        }, "json"
                    );

And my question is how to write all information from jason about model?For eg I want that in one paragraph tag will be one object with all information about it. How I can do this? Thanks for help Best Regards


Check out the format that the serializer spits out (via your browser or shell):

[{"pk": 1, "model":"modelname", "fields": {"fieldname": "fieldvalue", "fieldname2":"fieldvalue2"}}, ....]

So it's a list of arrays with pk, model, and fields which is an array.

$.getJSON("/myurl", 
    function(data) {
        // data is [{},{},{}]
        $.each(data, function(key, val) { 
            // val is { pk, model, {fields}}
            $("#output").append("Object id is: " + val.pk + ' of model: ' + val.model);
            $.each(val.fields, function(fieldname, field) {
                 $("#output").append(fieldname + ' : ' + field);
            }); 
        });
    }, "json");

<div id="output">

</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜