Sachin
Sachin

Reputation: 3782

How to return multiple JSON objects in an AJAX request in django

Currently I have a view which I render to a template and return it two query lists. My view is as shown below

def view_notifications(request,user_id):

    context_instance=RequestContext(request)
    user = User.objects.get(pk=user_id)

    user.profile.notifications = 0
    user.profile.save()

    notices = list(notifications.objects.filter(n_reciever=user.id, is_read=0).order_by('-time'))
    number = notifications.objects.filter(n_reciever=user.id, is_read=0).order_by('-time').count()

    if number < 5:
        old_notices = list(notifications.objects.filter(n_reciever=user.id, is_read=1).order_by('-time')[:5])
    else:
        old_notices = False

    notifications.objects.all().update(is_read = 1)

    return render_to_response('profiles/notifications.html', {'New_Notice': notices, 'Old_Notices':old_notices, 'number': number,},context_instance=RequestContext(request))

In my template I iterate through the two lists and give a background color to the list objects which are new as

<ul id="notification_list">
    <li id="first"></li>
    {% for notice in New_Notice %}
        <li class="new"> <a href="{{notice.n_sender.profile.get_absolute_url}}">{{ notice.n_sender.profile.url_name}} </a> {{notice.message}} your <a href="{{notice.object_url}}"> {{notice.object_type}}</a></li>
    {% endfor %}
    {% for notice in Old_Notices %}
        <li> <a href="{{notice.n_sender.profile.get_absolute_url}}">{{ notice.n_sender.profile.url_name}} </a> {{notice.message}} your <a href="{{notice.object_url}}"> {{notice.object_type}}<a/></li>
    {% endfor %}
</ul>

Now I want to do the same thing via an AJAX call and display these objects in a drop down list instead of a new page, so that the user can view the notifications there itself without navigating away. I cannot understand how I can send two JSON encoded object_lists. I know how to serialize an object list to JSON

data = serializers.serialize('json', notifications.objects.all())

But can I send two object_lists this way? Also I do not know how to display this JSON encoded object list in html. Can I access it the same way I access the objects in the template?

Please help

Upvotes: 2

Views: 7684

Answers (2)

dgel
dgel

Reputation: 16806

I would initiate an empty list, loop through the notices and append a dict to your list containing all of the required attributes. For example, your new_notices list may look like this:

[{'absolute_url': 'blah', 'url_name': 'blah', 'message': 'blah', 'type': 'blah'},
 {'absolute_url': 'foo', 'url_name': 'foo', 'message': 'foo', 'type': 'foo'}]

Once you have created a list for each set of notices (old and new), you can send them:

from django.utils import simplejson
from django.http import HttpResponse
...
json = simplejson.dumps({'old': old_notices, 'new': new_notices})
return HttpResponse(json, mimetype='text/json')

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599856

You want to send a single list, consisting of two elements, one for each list. You can do this by serializing to Python first, then dumping the whole lot to JSON.

data1 = serializers.serialize('python', notifications.objects.all())
data2 = serializers.serialize('python', foobar.objects.all())

data = simplejson.dumps([data1, data2])

(Or you could use a dictionary, if lookup by key would be easier in your javascript.)

Upvotes: 5

Related Questions