> > If you already fetched the queryset, `if obj in queryset` will make a new > database query
I don't see why we couldn't implement __contains__ to do a walk through _result_cache if it has been fetched? On Tue, 2 Jun 2020 at 10:13, Aymeric Augustin < [email protected]> wrote: > Hello Johan, > > You explained the upside. There's one downside to be aware of. If you > already fetched the queryset, `if obj in queryset` will make a new database > query, which will be slower than walking through the already fetched data > (unless the queryset is really large and the database really close in terms > of network latency). Making this change may throw off some assertNumQueries > tests. However, people who consider this a regression can do `obj in > list(queryset)` or perhaps `obj in iter(queryset)` to reuse the cached list. > > Judgment call: I think the upside is worth the downside and > backwards-incompatibility, so I'm in favor of the change. > > Best regards, > > -- > Aymeric. > > > > On 2 Jun 2020, at 10:57, Johan Schiff <[email protected]> wrote: > > Is there a specific reason Djangos QuerySet does not implement > __contains__? It doesn't seem very complicated to me, but maybe I'm missing > something. > > When checking if an object is in e queryset I always use code lite this: > if queryset.filter(pk=obj.pk).exists(): > > The pythonic way would be: > if obj in queryset: > > The way I understand it this latter version will fetch all objects from > queryset, while the first one is a much leaner db query. > > So, is there a reason QuerySet doesn't have a __contains__ method, or > would it be a good addition? My guess is that a lot of people use the "obj > in queryset" syntax, believing it does "the right thing". > > //Johan > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/88c83b8d-658c-47cc-9162-fcfebebe9c4a%40googlegroups.com > <https://groups.google.com/d/msgid/django-developers/88c83b8d-658c-47cc-9162-fcfebebe9c4a%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/570372E7-2949-426C-BAAE-DBEF1B17FBC0%40polytechnique.org > <https://groups.google.com/d/msgid/django-developers/570372E7-2949-426C-BAAE-DBEF1B17FBC0%40polytechnique.org?utm_medium=email&utm_source=footer> > . > -- Adam -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMyDDM21UY5u_sDyuSeNGY%3D4wOXXX4rSDR6CE1yYR_ZBmOoRSw%40mail.gmail.com.
