Re: Can't do any manipulation on a db object
Oh. Boy do I feel stupid. Thanks for all your help. I'll try to be less careless next time. On Oct 22, 8:43 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Tue, 2008-10-21 at 23:33 -0700, guruyaya wrote: > > This is actually worse > > You mean top-posting? Yes, it's a terrible habit and you should stop > doing it. > > > > > 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 Hello on top). > > > What am I missing? > > I'll bet you didn't change your template, so it's still trying to > iterate over postlist.all, rather than just postlist now. Sorry, I cut a > corner and didn't point out all the changes necessary, just pointing you > in the right direction. You're passing a list now, not a queryset, so > you'll need to adjust the template if you really want to make these > changes in the view like this (although, as I indicated, it's a pretty > uncommon pattern when you start to do things out of experimentation > mode, so you might want to avoid the hassle altogether). > > 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 -~--~~~~--~~--~--~---
Re: Can't do any manipulation on a db object
On Tue, 2008-10-21 at 23:33 -0700, guruyaya wrote: > This is actually worse You mean top-posting? Yes, it's a terrible habit and you should stop doing it. > > 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 Hello on top). > > What am I missing? I'll bet you didn't change your template, so it's still trying to iterate over postlist.all, rather than just postlist now. Sorry, I cut a corner and didn't point out all the changes necessary, just pointing you in the right direction. You're passing a list now, not a queryset, so you'll need to adjust the template if you really want to make these changes in the view like this (although, as I indicated, it's a pretty uncommon pattern when you start to do things out of experimentation mode, so you might want to avoid the hassle altogether). 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 -~--~~~~--~~--~--~---
Re: Can't do any manipulation on a db object
Sorry if it appears twice. My message seem to have vanished. What I said was that this is even worse 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 = list(a) a[0].title = 'hihi' a[0].content = 'haha' return RenderToResponse('blog/showlist.html',{'postlist': a}); It just shows nothing. Not even the posts I didn't change. Any ideas? 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 -~--~~~~--~~--~--~---
Re: Can't do any manipulation on a db object
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 Hello 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 -~--~~~~--~~--~--~---
Re: Can't do any manipulation on a db object
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 -~--~~~~--~~--~--~---
Can't do any manipulation on a db object
I'm kinda new to django, and python, so I'm probably gonna make myself look a bit stupid. Here goes anyway: As a simple django (1.0) project, (using python 2.6) I've decided to write a blog application. Here it goes: blog/urls.py -- from django.conf.urls.defaults import * urlpatterns = patterns('myblog.blog', (r'','views.listposts'), ) blog/models.py --- from django.conf.urls.defaults import * urlpatterns = patterns('myblog.blog', (r'','views.listposts'), ) blog/models.py from django.db import models from django.http import HttpResponse from tinymce.widgets import TinyMCE import re class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField('date written',auto_now_add=True, null=True) change_date = models.DateTimeField('date changed',auto_now=True, null=True) def __unicode__(self): return self.title def save(self, force_insert=False, force_update=False): ... Here come some long lines of code I think is irrelevant, but ask me if you think they are 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}); blog/showlist.html --- Hello {% for post in postlist.all %} {{ post.title }} {{ post.content|safe }} {% endfor %} 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. Any ideas? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---