Skip to content

Mass join for M2M fields #567

@northpowered

Description

@northpowered

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions