piccolo icon indicating copy to clipboard operation
piccolo copied to clipboard

Mass join for M2M fields

Open northpowered opened this issue 3 years ago • 5 comments

#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>]

northpowered avatar Jul 20 '22 09:07 northpowered

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!

dantownsend avatar Jul 22 '22 10:07 dantownsend

Any thoughts? ;-)

northpowered avatar Jan 30 '23 19:01 northpowered

@dantownsend Pls, run linters, I hope I fixed typing problems for Python < 3.10

northpowered avatar Mar 06 '23 10:03 northpowered

@northpowered I've fixed the linter errors - the tests are running now.

dantownsend avatar Mar 06 '23 14:03 dantownsend

@dantownsend mb we can merge #566 ?

northpowered avatar Aug 03 '23 06:08 northpowered