This is actually worse blog/models.py ---------------- from django.shortcuts import render_to_response as RenderToResponse from myblog.blog.models import Post def listposts(request): a = Post.objects.all() a = list(a) a[0].title = 'hihi' a[0].content = 'haha' return RenderToResponse('blog/showlist.html',{'postlist': a});
Just shows me a blank page (with <h1>Hello</h1> on top). What am I missing? On Oct 22, 8:17 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Tue, 2008-10-21 at 22:30 -0700, guruyaya wrote: > > [...] > > > blog/views.py > > ----------- > > from django.shortcuts import render_to_response as RenderToResponse > > from myblog.blog.models import Post > > def listposts(request): > > a = Post.objects.all() > > a[0].title = 'hihi' > > a[0].content = 'haha' > > print a[0].title > > print a[0].content > > return RenderToResponse('blog/showlist.html',{'postlist': a}); > > [...] > > > As you may have guest, I expected the first post title to be 'hihi' > > and it's content to be 'haha'. Well... it's not. I still get the same > > post I have in my database. Yet the "print" lines give me the right > > post content in the server debug messages. > > That's because a[0] is not updating 'a' in place. Instead, it's > returning a new queryset, that contains a slice of the original one and > then that clone is updated. You are still passing the original 'a' to > the template, however. > > If you want to do updating in place, you'll need to convert 'a' to an > object that supports those types of updates, namely a list: > > a = list(a) > a[0].title = 'Now updated in place because a is a list!' > > Realise that this isn't the sort of thing that is normally going to be a > problem because it's just not that normal a usage pattern. Typically, > you will either be extracting a bunch of data in a queryset and passing > that to a template, *or* you will be iterating through a queryset or > getting one particular object, updating the object(s) and then saving > them. Not mixing both at once. > > Regards, > Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---