Open pmg103 opened 4 years ago
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)
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.
Oh I can use Q-expressions of course:
serialization_spec = [
{'reportgenerationbatch_set': Filtered(Q(session=None), [
'comparison_group',
'report_templates',
])},
Need a plugin to allow subqueries to be filtered. Something like:
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?