Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mass join for M2M fields #567

Open
northpowered opened this issue Jul 20, 2022 · 5 comments
Open

Mass join for M2M fields #567

northpowered opened this issue Jul 20, 2022 · 5 comments

Comments

@northpowered
Copy link

#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
Copy link
Member

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
Copy link
Author

Any thoughts? ;-)

@northpowered
Copy link
Author

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

@dantownsend
Copy link
Member

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

@northpowered
Copy link
Author

@dantownsend mb we can merge #566 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants