feat: rewrote the method for getting m2m objects#2205
Conversation
|
Thanks for the patch. The change looks useful for enabling
Overall, the direction looks promising, but these cases seem worth verifying with focused regression tests. |
|
@waketzheng
|
|
Thanks for the update. Consider this: The previous concerns around One remaining edge case may be worth confirming: in the new through-table lookup, the parent PKs are converted with .where(forward_field.isin(tuple(related_objects_by_pks)))For A regression test in @pytest.mark.asyncio
async def test_prefetch_related(db, m2m_uuid_models):
UUIDPkModel, UUIDM2MRelatedModel = m2m_uuid_models
one = await UUIDPkModel.create()
two = await UUIDM2MRelatedModel.create()
await one.peers.add(two)
fetched = await UUIDPkModel.get(pk=one.pk).prefetch_related("peers")
assert list(fetched.peers) == [two]It may be safer to convert the related PKs before the through-table query as well, analogous to |
|
@waketzheng Thanks! I corrected it. |
Description
The
_prefetch_m2m_relationmethod of theBaseExecutorclass has been rewritten. Now objects are received directly through the specifiedqueryset, which allows you to correctly useselect_related,annotate, etc.Motivation and Context
prefetch_related(m2m) functionality becomes like inDjango.How Has This Been Tested?
Added tests to
tests/test_prefetching.pywith verification of receiving data fromannotateandselect_related.Checklist: