0

here is my django view

@csrf_exempt
@login_required
def get_playlists_for_user(request):
    """
    gets all playlists for user
    """
    logging.info('getting playlist for user - ' + str(request.user))
    playlists = UserPlaylist.objects.get_all_playlists_for_user(request.user)
    logging.info('user=%s, playlists=%s'%(request.user, playlists))
    return HttpResponse(playlists)

Here is how jQuery calls it via ajax

$.ajax({
        url: '/getUserPlaylists',
        success: function(response, textStatus, jqXHR){

            console.log(response);
        },
        error: function(response, textStatus, jqXHR) {

        }
});

the view return the list of Playlist objects

playlists=[<Playlist id:1, name:first, date_created:2012-08-05 06:28:31.954623+00:00, deleted:False>, <Playlist id:2, name:my, date_created:2012-08-06 12:47:13.023537+00:00, deleted:False>, <Playlist id:3, name:new, date_created:2012-08-06 12:48:45.708277+00:00, deleted:False>, <Playlist id:5, name:second, date_created:2012-08-06 21:19:33.050187+00:00, deleted:False>]

The schema model for Playlist is

class Playlist(models.Model):
    name = models.CharField(max_length=30)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField(default=False)

When I log them in firebug I see them as

Playlist objectPlaylist objectPlaylist objectPlaylist object

How can I read these objects in jQuery?

1 Answer 1

2

You need to return the Python objects in a form javascript can understand which is usually json (javascript object notation). So in your view, convert your django queryset into json, using django's serialisers:

from django.core import serializers
...
json = serializers.serialize('json', playlists)
return HttpResponse(json, mimetype="application/json")

and in your ajax:

$.ajax({
       url: '/getUserPlaylists',
       dataType: "json",
       success: function(response, textStatus, jqXHR){
           for(var i = 0; i < response.length; i++ ){
               var playlist = response[i]['fields'];
               // Do something now with your playlist object
               console.log(playlist.name);
           }
       },
       error: function(response, textStatus, jqXHR) {
       }
});

this will mean the response in your ajax success will be something like this (a list of javascript objects) :

[{ "model" : "playlist.Playlist", "pk" : 1, "fields" : { "name" : "...", ... } }]
Sign up to request clarification or add additional context in comments.

2 Comments

Right idea, but json.dumps(playlists) won't do anything useful, as json does not know how to serialize a queryset.
Yea, just noticed it was a QS, not an object. Just going to update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.