Re: QuerySet Behaviour Question (Django 1.1)
Hi On Mon, Jun 28, 2010 at 4:37 PM, Jeff wrote: > Hi, > > I have a question concerning some queryset behaviour in Django 1.1. > > In this example, I have an article model that can contain 1 or more > authors who are users registered through Django's auth system. > > I get the following output when playing around with an article in a > shell where I have three authors: > article.authors.all() > [, , ] article.authors.all()[0] > article.authors.all()[1] > article.authors.all()[2] > article.authors.all()[0:1] > [] article.authors.all()[1:2] > [] article.authors.all()[2:3] > [] > > I'm curious why article.authors.all()[0] does not return > and instead returns . I'm also curious as to why jeff> appears twice when slicing queries and it doesn't when > requesting all results. > > Thank you in advance for your time! > Simple... User does not have a default ordering, therefore the database is free to return the results in random order (the order may even be different for subsequent queries as you have seen) You should add an .order_by() clause if you need a stable ordering of Users, f.e. User.objects.order_by('username') -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: QuerySet Behaviour Question (Django 1.1)
Yes I am using PostgreSQL and I didn't add an order by clause. Adding the order by clause fixed the problem. Thank you very much Karen! Jeff On Jun 28, 10:46 am, Karen Tracey wrote: > On Mon, Jun 28, 2010 at 10:37 AM, Jeff wrote: > > Hi, > > > I have a question concerning some queryset behaviour in Django 1.1. > > > In this example, I have an article model that can contain 1 or more > > authors who are users registered through Django's auth system. > > > I get the following output when playing around with an article in a > > shell where I have three authors: > > > >>> article.authors.all() > > [, , ] > > >>> article.authors.all()[0] > > > > >>> article.authors.all()[1] > > > > >>> article.authors.all()[2] > > > > >>> article.authors.all()[0:1] > > [] > > >>> article.authors.all()[1:2] > > [] > > >>> article.authors.all()[2:3] > > [] > > > I'm curious why article.authors.all()[0] does not return > > and instead returns . I'm also curious as to why > jeff> appears twice when slicing queries and it doesn't when > > requesting all results. > > > Thank you in advance for your time! > > I'm guessing you are using PostgreSQL? And you have no ordering specified on > the authors model? This is typical behavior for that situation. These > queries pull results from the DB by using OFFSET and LIMIT...if there is no > ORDER BY in the query then the DB can (and PostgreSQL definitely will) > return "unpredictable" results for them. > See:http://developer.postgresql.org/pgdocs/postgres/queries-limit.html > > Karen > --http://tracey.org/kmt/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: QuerySet Behaviour Question (Django 1.1)
On Mon, Jun 28, 2010 at 10:37 AM, Jeff wrote: > Hi, > > I have a question concerning some queryset behaviour in Django 1.1. > > In this example, I have an article model that can contain 1 or more > authors who are users registered through Django's auth system. > > I get the following output when playing around with an article in a > shell where I have three authors: > > >>> article.authors.all() > [, , ] > >>> article.authors.all()[0] > > >>> article.authors.all()[1] > > >>> article.authors.all()[2] > > >>> article.authors.all()[0:1] > [] > >>> article.authors.all()[1:2] > [] > >>> article.authors.all()[2:3] > [] > > I'm curious why article.authors.all()[0] does not return > and instead returns . I'm also curious as to why jeff> appears twice when slicing queries and it doesn't when > requesting all results. > > Thank you in advance for your time! > I'm guessing you are using PostgreSQL? And you have no ordering specified on the authors model? This is typical behavior for that situation. These queries pull results from the DB by using OFFSET and LIMIT...if there is no ORDER BY in the query then the DB can (and PostgreSQL definitely will) return "unpredictable" results for them. See: http://developer.postgresql.org/pgdocs/postgres/queries-limit.html Karen -- http://tracey.org/kmt/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
QuerySet Behaviour Question (Django 1.1)
Hi, I have a question concerning some queryset behaviour in Django 1.1. In this example, I have an article model that can contain 1 or more authors who are users registered through Django's auth system. I get the following output when playing around with an article in a shell where I have three authors: >>> article.authors.all() [, , ] >>> article.authors.all()[0] >>> article.authors.all()[1] >>> article.authors.all()[2] >>> article.authors.all()[0:1] [] >>> article.authors.all()[1:2] [] >>> article.authors.all()[2:3] [] I'm curious why article.authors.all()[0] does not return and instead returns . I'm also curious as to why appears twice when slicing queries and it doesn't when requesting all results. Thank you in advance for your time! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.