mellow-yellow
mellow-yellow

Reputation: 1810

beginner: django serializers.serialize

Disclaimer: I am new to django but have coded in Drupal

I need to return JSON data to my webpage. In my code,

x    = serializers.serialize('json', Temp.objects.all())

returns

[{"pk": 1, "model": "wizard.temp", "fields": {"p": "message", "k": null, "m": null, "v": "PLEASE WAIT -- Counting and enumerating your images..."}}]'

How do I remove the leading and trailing brackets -- that is, the [ and ] ?

It seems only Firefox understands data[0]['pk'], so I'd like to instead do data['pk'].

Upvotes: 0

Views: 845

Answers (1)

David Wolever
David Wolever

Reputation: 154464

You want to be serializing only one instance of Temp. For example, to serialize only the first instance:

serializers.serialize('json', Temp.objects.all()[0])

Upvotes: 1

Related Questions