2

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

2 Answers 2

5

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.)

Sign up to request clarification or add additional context in comments.

4 Comments

Wow much better than what I was going to post, although you definitely want to use a dictionary instead of a list.
Would this capture the methods on foreign keys such as notice.n_sender.profile.get_absolute_url?
Lookup by key would definitely by easier, I am new to Python so could you please tell me how I can make a dictionary and secondly I do need the complete objects of the foreign key. In this case I need to whole object of n_sender
I would loop through the models and create the dictionary manually, making sure you have all the attributes you need. (As I suggested in my answer)
2

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')

2 Comments

Can you also tell me how can I access these objects in my AJAX success call and add them to proper html tags
Another thing is that for n_sender I may not need the whole object but I need the get_absolute_url and few more fields of the sender. I know I need to add to the dictionary that I am sending, but how can I append these values to the query_set that I obtain. Can you show me how I can construct a dictionary that has the name-value pairs of the query_set and the required name value pairs from the corresponding n_sender object in the same dictionary

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.