Re: database API filter() vs get()
On 2007-12-17 22:41:08 -0700, Kenneth Gonsalves <[EMAIL PROTECTED]> said: > > > On 18-Dec-07, at 10:33 AM, MrJogo wrote: > >> What's the practical difference between calling >> DB.objects.filter(**kwargs) and DB.objects.get(**kwargs) (where >> **kwargs can be whichever paramenters)? I understand that filter >> returns a QuerySet, but I don't really get how that affects me or my >> code. > > get will return precisely one object. whereas filter will return a > list of objects (list may contain only one object or be empty. So the > difference is that if you do > > a = ...get() > you can write a.id > a= ...filter() > you would have to write a[0].id (if you are expecting only one object > to be returned) One other thing to note is .filter returns a QuerySet object. What this means is that the database won't be hit until it is evaluated. Ex. it is iterated over in a for loop, you do a[0].id, or you provide a step in the slice a[::2]. .get will actually hit the database when called. -- Brian Rosner http://oebfare.com --~--~-~--~~~---~--~~ 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: database API filter() vs get()
I understand now. Thanks. On Dec 17, 9:11 pm, Kenneth Gonsalves <[EMAIL PROTECTED]> wrote: > On 18-Dec-07, at 10:33 AM, MrJogo wrote: > > > What's the practical difference between calling > > DB.objects.filter(**kwargs) and DB.objects.get(**kwargs) (where > > **kwargs can be whichever paramenters)? I understand that filter > > returns a QuerySet, but I don't really get how that affects me or my > > code. > > get will return precisely one object. whereas filter will return a > list of objects (list may contain only one object or be empty. So the > difference is that if you do > > a = ...get() > you can write a.id > a= ...filter() > you would have to write a[0].id (if you are expecting only one object > to be returned) > > -- > > regards > kghttp://lawgon.livejournal.comhttp://nrcfosshelpline.in/web/ > Foss Conference for the common man:http://registration.fossconf.in/web/ --~--~-~--~~~---~--~~ 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: database API filter() vs get()
On 18-Dec-07, at 10:33 AM, MrJogo wrote: > What's the practical difference between calling > DB.objects.filter(**kwargs) and DB.objects.get(**kwargs) (where > **kwargs can be whichever paramenters)? I understand that filter > returns a QuerySet, but I don't really get how that affects me or my > code. get will return precisely one object. whereas filter will return a list of objects (list may contain only one object or be empty. So the difference is that if you do a = ...get() you can write a.id a= ...filter() you would have to write a[0].id (if you are expecting only one object to be returned) -- regards kg http://lawgon.livejournal.com http://nrcfosshelpline.in/web/ Foss Conference for the common man: http://registration.fossconf.in/web/ --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
database API filter() vs get()
What's the practical difference between calling DB.objects.filter(**kwargs) and DB.objects.get(**kwargs) (where **kwargs can be whichever paramenters)? I understand that filter returns a QuerySet, but I don't really get how that affects me or my code. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---