reactive-python / reactpy-django

It's React, but in Python. Now with Django integration.
https://reactive-python.github.io/reactpy-django/
MIT License
323 stars 18 forks source link

`use_query` does not properly pre-fetch some model fields #110

Closed Archmonger closed 1 year ago

Archmonger commented 1 year ago

Current Situation

ManyToMany fields are not properly prefetched, thus causing SynchronousOnlyOperation exceptions.

For example, in this case.

Proposed Actions

Determine why the current prefetch logic doesn't work on this use case.

rmorshea commented 1 year ago

It looks like one-to-many and many-to-many relationships return relationship managers. Should be as simple as calling the get_queryset() and recursively resolving values.

rmorshea commented 1 year ago

Ok, as discovered in that draft PR, get_queryset() does not actually cache anything. After doing a bit of research, it seems like this is something that needs to be managed by users with prefetch_related or select_related. There's a prefect_related_objects function that can work on model instances rather than queries, but I'm not really sure how that could be used automatically.

Archmonger commented 1 year ago

Looks like prefect_related_objects was exactly what we needed.