piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.35k stars 88 forks source link

Mass join for M2M fields #567

Open northpowered opened 1 year ago

northpowered commented 1 year ago

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>]
dantownsend commented 1 year ago

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!

northpowered commented 1 year ago

Any thoughts? ;-)

northpowered commented 1 year ago

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

dantownsend commented 1 year ago

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

northpowered commented 11 months ago

@dantownsend mb we can merge #566 ?