queryset.iterator() and prefetch_related()

83 views
Skip to first unread message

1337 Shadow Hacker

unread,
Apr 28, 2020, 12:36:13 PM4/28/20
to Django developers (Contributions to Django itself)
Hi all,

I notice a piece of code inside ModelChoiceIterator that seems to keep going a bit back and forth, currently it looks like this:
        # Can't use iterator() when queryset uses prefetch_related()
        if not queryset._prefetch_related_lookups:
            queryset = queryset.iterator()
But before it looked like this:

        # Can't use iterator() when queryset uses prefetch_related()
        if not queryset._prefetch_related_lookups and queryset._result_cache is None:
             queryset = queryset.iterator()

Anyway, my issue is that it forces you 

1337 Shadow Hacker

unread,
Apr 28, 2020, 12:40:09 PM4/28/20
to django-d...@googlegroups.com
Sorry I sent the mail prior to finishing, redoing fully from here:
Anyway, if you want to implement your own QuerySet class from scratch, which is the case of django-querysetsequence, and that's pretty useful to feed ModelChoiceFields, as long as you prefix object ids with content type ids which is pretty trivial.

Do you think it would be acceptable to start an effort with the objective of making django.forms.models rely purely on a public API of QuerySet ?

In this case, how would you accept to see that changed ?

Thanks in advance

Have a great day

1337 Shadow Hacker

unread,
Jun 11, 2020, 8:59:09 AM6/11/20
to django-d...@googlegroups.com
We've decided to open a ticket and MR for it: https://github.com/percipient/django-querysetsequence/issues/67 (that would happen this weekend)

If there's any objection please let us know.

Have a great day

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

charettes

unread,
Jun 11, 2020, 9:37:41 AM6/11/20
to Django developers (Contributions to Django itself)
FWIW there's ungoing work to allow iterator() to use prefetch_related() which would allow us to lift this restriction.


Cheers,
Simon

1337 Shadow Hacker

unread,
Jun 11, 2020, 9:44:32 AM6/11/20
to django-d...@googlegroups.com
Well that's good to know, thank you charettes !

Does that mean that the piece of code from forms that's using "private" API from QuerySet is going away in the next Django version ?

In this case, we probably don't have to do anything on our end ?

charettes

unread,
Jun 11, 2020, 9:56:28 AM6/11/20
to Django developers (Contributions to Django itself)
The code could possibly be removed if the PR gets merged in time for 3.2 yes.

1337 Shadow Hacker

unread,
Jun 11, 2020, 9:59:53 AM6/11/20
to django-d...@googlegroups.com
Awesome, thank you so much charettes.

We're implementing the "private" attribute meanwhile so there's no rush.

Have a great day

Reply all
Reply to author
Forward
0 new messages