ForeignKey related bug
Hi, While building a blog type application, I seem to have stumbled upon a bug. In a nut shell all object attributes have become read only when I fetch the object from a query set which is fetched using the xxx_set() API. Please see the pdb trace for details. I have added comments to depict the erroneous logs. (pdb was started from within a unit test). I do get similar behavior when using the shell. Any suggestions for debugging this one? regards, CP class Blog (models.Model): title = models.CharField (maxlength = 300, default=None) blog_id = models.CharField (unique=True, null=False, blank=False, default=None, maxlength=64) class Post (models.Model): blog = models.ForeignKey (Blog) post_id = models.CharField (maxlength = 64, null=False, unique=True) title = models.CharField (maxlength = 200, null=False) pdb logs - (Pdb) blog.post_set.all() [] (Pdb) m = blog.post_set.all() (Pdb) m[0].title 'check accidents' (Pdb) print m[0] Blogger Post id: 6497585470310212022, title: check accidents (Pdb) m[0].title = "dfdf" (Pdb) m[0].title #NOTE: that the title does not change 'check accidents' (Pdb) print m[0] Blogger Post id: 6497585470310212022, title: check accidents (Pdb) m[0].__dict__ {'blog_id': 2, 'title': 'check accidents', 'post_id': '6497585470310212022', 'id': 1} (Pdb) m[0].__dict__["title"] = "dfdf" (Pdb) m[0].__dict__ {'blog_id': 2, 'title': 'check accidents', 'post_id': '6497585470310212022', 'id': 1} (Pdb) ps = Post.objects.all() (Pdb) ps [] (Pdb) ps[0].title = "dfdF" (Pdb) ps [] (Pdb) python logs --- >>> blog = Blog.objects.get (url__contains = "abc") >>> m = blog.post_set.all() >>> m[0].title 'FDfdF' >>> m[0].title="dfdF" >>> m[0] >>> m[0].title 'FDfdF' --~--~-~--~~~---~--~~ 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: ForeignKey related bug
Chirayu Patel wrote: > Hi, > > While building a blog type application, I seem to have stumbled upon a > bug. In a nut shell all object attributes have become read only when I > fetch the object from a query set which is fetched using the xxx_set() > API. > > Please see the pdb trace for details. I have added comments to depict > the erroneous logs. (pdb was started from within a unit test). I do > get similar behavior when using the shell. > > Any suggestions for debugging this one? This may not be all of your problem, but pdb had a bug (at least Python 2.4, maybe it's gone in 2.5) wherein setting a variable at a breakpoint worked if and only if you didn't reexamine it after setting it. E.g., this worked: (Pdb) foobar = "dfdf" (Pdb) c But this did not: (Pdb) foobar = "dfdf" (Pdb) foobar 'old value' (Pdb) c There used to be a post on someone's blog about this, but darned if I can find it now. John --~--~-~--~~~---~--~~ 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: ForeignKey related bug
> > A little weird, maybe someone else can explain exactly what's going > on. Presumably a QuerySet slice is somehow not being treated as the > object itself. Hi, As Eric correctly pointed out that slicing a QuerySet is not "sticky". Whenever you take a slice of an exisiting QuerySet, Django clones it dynamically to a new QuerySet. So every time you reference m[0] you get a slice from a *clone* of the QuerySet m. It follows that this clone has a different instance of m[0] from the previous m[0]. As Eric suggested, save m[0] first to a variable if you need to act on a known single instance of that object for multiple operations. -Rajesh D --~--~-~--~~~---~--~~ 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: ForeignKey related bug
> While building a blog type application, I seem to have stumbled upon a > bug. In a nut shell all object attributes have become read only when I > fetch the object from a query set which is fetched using the xxx_set() > API. Hi Chirayu, Looks like the problem is not the FOO_set() manager, but setting attributes on an object accessed via a QuerySet slice. Try putting m[0] by itself into a variable, and then setting attributes on that variable: it works. Directly setting attributes by using m[0].title = XXX doesn't seem to stick, however. x = m[0] x.title = 'dfdf' x.title # gets me u'dfdf' A little weird, maybe someone else can explain exactly what's going on. Presumably a QuerySet slice is somehow not being treated as the object itself. Eric --~--~-~--~~~---~--~~ 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: ForeignKey related bug
> While building a blog type application, I seem to have stumbled upon a > bug. In a nut shell all object attributes have become read only when I > fetch the object from a query set which is fetched using the xxx_set() > API. Hi Chirayu, Looks like the problem is not the FOO_set() manager, but setting attributes on an object accessed via a QuerySet slice. Try putting m[0] by itself into a variable, and then setting attributes on that variable: it works. Directly setting attributes by using m[0].title = XXX doesn't seem to stick, however. x = m[0] x.title = 'dfdf' x.title # gets me u'dfdf' A little weird, maybe someone else can explain exactly what's going on. Presumably a QuerySet slice is somehow not being treated as the object itself. Eric --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
ForeignKey related bug
Hi, While building a blog type application, I seem to have stumbled upon a bug. In a nut shell all object attributes have become read only when I fetch the object from a query set which is fetched using the xxx_set() API. Please see the pdb trace for details. I have added comments to depict the erroneous logs. (pdb was started from within a unit test). I do get similar behavior when using the shell. Any suggestions for debugging this one? regards, CP class Blog (models.Model): title = models.CharField (maxlength = 300, default=None) blog_id = models.CharField (unique=True, null=False, blank=False, default=None, maxlength=64) class Post (models.Model): blog = models.ForeignKey (Blog) post_id = models.CharField (maxlength = 64, null=False, unique=True) title = models.CharField (maxlength = 200, null=False) pdb logs - (Pdb) blog.post_set.all() [] (Pdb) m = blog.post_set.all() (Pdb) m[0].title 'check accidents' (Pdb) print m[0] Blogger Post id: 6497585470310212022, title: check accidents (Pdb) m[0].title = "dfdf" (Pdb) m[0].title #NOTE: that the title does not change 'check accidents' (Pdb) print m[0] Blogger Post id: 6497585470310212022, title: check accidents (Pdb) m[0].__dict__ {'blog_id': 2, 'title': 'check accidents', 'post_id': '6497585470310212022', 'id': 1} (Pdb) m[0].__dict__["title"] = "dfdf" (Pdb) m[0].__dict__ {'blog_id': 2, 'title': 'check accidents', 'post_id': '6497585470310212022', 'id': 1} (Pdb) ps = Post.objects.all() (Pdb) ps [] (Pdb) ps[0].title = "dfdF" (Pdb) ps [] (Pdb) python logs --- >>> blog = Blog.objects.get (url__contains = "abc") >>> m = blog.post_set.all() >>> m[0].title 'FDfdF' >>> m[0].title="dfdF" >>> m[0] >>> m[0].title 'FDfdF' --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---