Re: about QuerySet
do you mean that queryset will query database every time i call user[0]? On 1月30日, 午前9:29, akaariai wrote: > On Jan 20, 10:12 am, newme wrote: > > > user = User.objects.filter(pk="") > > user is a QuerySet > > > every time i call user[0], i returns a different reference. > > > maybe it should be a python question. > > i want to know i QuerySet does that. > > The reason is that Django does not do "instance caching". That is, you > will get different reference each time from a queryset because that is > how it works. If you need to use the same instance, then you need to > store that somewere yourself. This is all the help I can give based on > your question. > > - Anssi -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: about QuerySet
so it means when i call user[1] after user[0], it is possible that i will get same record if someone else insert a new record into database between 2 calls. On Feb 1, 5:34 pm, akaariai wrote: > On Feb 1, 9:36 am, newme wrote: > > > do you mean that queryset will query database every time i call > > user[0]? > > Yes. That is exactly what happens: > In [7]: qs[0] > Out[7]: > In [9]: print connection.queries > [{'time': '0.011', 'sql': 'SELECT ... FROM "organisaatio_osa" LIMIT > 1'}] > > In [10]: qs[0] > Out[10]: > In [11]: print connection.queries > [{'time': '0.011', 'sql': 'SELECT ... FROM "organisaatio_osa" LIMIT > 1'}, > {'time': '0.001', 'sql': 'SELECT ... FROM "organisaatio_osa" LIMIT > 1'}] > > If you do not want this to happen, you can evaluate your queryset into > a list first by: > objlist = list(qs[0:wanted_limit]) > and now objlist is just a regular Python list. > > The lazy evaluation of querysets can be a little surprising sometimes. > Using django-debug-toolbar or just settings.DEBUG = True, and then > print connection.queries is recommended :) > > - Anssi -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: about QuerySet
thank you. then a basic python question. how does queryset implement it? does python also allow operator (e.g. []) overloading? On Feb 3, 5:29 pm, akaariai wrote: > On Feb 3, 5:52 am, newme wrote: > > > so it means when i call user[1] after user[0], it is possible that i > > will get same record if someone else insert a new record into database > > between 2 calls. > > Actually, there doesn't need to be an insert between the calls if you > don't use .order_by(). Technically, without an ORDER BY the database > is free to return the records in any order it wishes, even if there > are no inserts in between. > > I think you really should fetch all the needed objects in one go. That > is the correct way to do what you need. And in addition it is more > efficient. > > - Anssi -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: about QuerySet
thank you so much. On Feb 7, 8:58 pm, Daniel Roseman wrote: > On Tuesday, 7 February 2012 11:36:04 UTC, newme wrote: > > > thank you. > > then a basic python question. > > how does queryset implement it? > > does python also allow operator (e.g. []) overloading? > > It's not usually called overloading in Python, but yes. Classes can define > a `__getitem__` method which determines what to do when the square-bracket > notation is used. You can see the code > here:https://code.djangoproject.com/browser/django/trunk/django/db/models/... > -- > DR. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
about QuerySet
user = User.objects.filter(pk="") user is a QuerySet every time i call user[0], i returns a different reference. maybe it should be a python question. i want to know i QuerySet does that. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
queryset[] returns different reference every time it is called
users = User.objects.filter(pk=1) user0 = user[0] user1 = user[1] user0 is user1 returns false it seems [] returns different reference every time it is called? how could this be done? i know it's maybe a python question, if i post it in a wrong group, i'm sorry. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.