Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Plugin: Filter 1:M relations #26

Open
pmg103 opened this issue Nov 1, 2019 · 3 comments
Open

Plugin: Filter 1:M relations #26

pmg103 opened this issue Nov 1, 2019 · 3 comments

Comments

@pmg103
Copy link
Contributor

pmg103 commented Nov 1, 2019

Need a plugin to allow subqueries to be filtered. Something like:

serialization_spec = [
    {'reportgenerationbatch_set': Filter([
        'comparison_group',
        'report_templates',
    ], session=None)},

The filter would be any valid django filter as it would be applied to the inner queryset.

I'd prefer it if the filter (session=None in this case) appeared BEFORE the child serialization spec but kwargs must appear last in python. Could pass a dictionary as first arg?

NOTE: Do we need to use a new key, perhaps using to_attr, so that it doesnt clash with any other prefetches done anywhere else in the tree?

@pmg103
Copy link
Contributor Author

pmg103 commented Nov 4, 2019

It could perhaps be done as a higher order function.

Usage would look like:

serialization_spec = [
    {'reportgenerationbatch_set': filter(session=None)([
        'comparison_group',
        'report_templates',
    ])},

Implementation would look like:

class Filter(SerializationSpecPlugin):
    def __init__(self, filter, serialization_spec):
        self.filter = self.filter
        self.serialization_spec = serialization_spec

    def modify_queryset(self, queryset):
        return queryset.filter(**self.filter)

    def get_value(self, instance):
        return getattr(instance, self.key)


def filter(**kwargs):
    return lambda spec: return Filter(kwargs, spec)

@RealOrangeOne
Copy link
Contributor

Personally I think the API looks nicer as a class rather than HoF, as the HoF doesn't really gain anything.

RE to_attr, I don't think it matters? Assuming the entire serialization tree is SSM, it doesn't matter what they key is called, as it's only the library which will be using it.

@pmg103
Copy link
Contributor Author

pmg103 commented Nov 13, 2019

Oh I can use Q-expressions of course:

serialization_spec = [
    {'reportgenerationbatch_set': Filtered(Q(session=None), [
        'comparison_group',
        'report_templates',
    ])},

@pmg103 pmg103 changed the title Plugin: Filter 1:M relations Plugin: Filter 1:M relations -- FIXED: NEEDS TESTS Nov 14, 2019
@pmg103 pmg103 changed the title Plugin: Filter 1:M relations -- FIXED: NEEDS TESTS Plugin: Filter 1:M relations Jan 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants