Mass join for M2M fields
#566 PR Adds join_m2m method for a Table class, which runs get_m2m() method for all M2M fields of object. Can be useful for complex PyDantic models in READ actions. Returns empty list() for an attribute, if there are no relations to this object.
Optional, you can include or exclude fields to define which attrs should be joined. Setting either include_fields, and exclude_fields will raise AssertionError.
Model example:
class Band(Table):
#some fields...
genres = m2m.M2M(LazyTableReference("BandtoGenre", module_path=__name__))
concerts = m2m.M2M(LazyTableReference("BandToConcert", module_path=__name__))
Usage:
>>> band = await Band.objects().get(Band.name == "Pythonistas")
>>> await band.join_m2m()
>>> band.genres
#[<Genre: 1>, <Genre: 2>]
>>> band.concerts
#[<Concert: 1>,<Concert: 2>,<Concert: 3>]
#include_fields example:
>>> await band.join_m2m(include_fields=['genres'])
>>> band.genres
#[<Genre: 1>, <Genre: 2>]
>>> band.concerts
#[]
#exclude_fields example:
>>> await band.join_m2m(exclude_fields=['genres'])
>>> band.genres
#[]
>>> band.concerts
#[<Concert: 1>,<Concert: 2>,<Concert: 3>]
It's an interesting idea - thanks.
I think we do need something like this. I'm just working my way through some other PRs at the moment, but hope to get to this soon!
Any thoughts? ;-)
@dantownsend Pls, run linters, I hope I fixed typing problems for Python < 3.10
@northpowered I've fixed the linter errors - the tests are running now.
@dantownsend mb we can merge #566 ?