Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make Django queries retrieve updated information from database?
    primarykey
    data
    text
    <p>For 2 days i have been stucked at transaction problem in my Django project. I'm developing a messenger system and when i send a message to some user, the sent message appears correctly in outbox div (with jQuery/Ajax, not refresh page). Well, If i send another message, the new message which appears in outbox now is the same as previous sent.</p> <p>All messages i send are like the first. In database table the messages are well saved, so i guess problem comes from query.</p> <p>The query is next:</p> <pre><code>@login_required(login_url='/') def getmessage(request, mode=None): if request.is_ajax(): if request.method == "GET": # Retrieve last message sent by user Message.objects.update() q = Message.objects.filter(sender=request.user).order_by('send_at')[:1] response = {} for sent in q: sent_message = {} sent_message['recipient'] = sent.recipient.username print sent.recipient.username sent_message['body'] = sent.body response.update({'sent': sent_message}) # Make json object json = simplejson.dumps(response) # Send answer to client side return HttpResponse(json, mimetype='application/json') else: # No POST request, redirecting return HttpResponseRedirect('/messenger/') else: # No AJAX request, redirecting return HttpResponseRedirect('/messenger/') </code></pre> <p>let's pay attention to line "print sent.recipient.username". This line always print the first message recipient name.</p> <p>I have been reading some threads around stackoverflow talking about this problem, but handling manually the transactions i haven't got any success. Even making "Message.objects.update()" before the query it doesn't work.</p> <p>Any suggestion? It's very annoying to not understand how does it happening.</p> <p>If you need more information like model code or whatever, tell it to me.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload