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

Allow for data to be prefetched and then accessible elsewhere in the tree #22

Open
pmg103 opened this issue Oct 21, 2019 · 2 comments
Open

Comments

@pmg103
Copy link
Contributor

pmg103 commented Oct 21, 2019

Something like this:

  serialization_spec = [
    Cache([
      {'organisation': [ 'id', 'name' ]}
    ]),
    {'users': [
        {'calculated_value': using_cache(UserCalculatedValue())}
    ]}
  ]
@pmg103
Copy link
Contributor Author

pmg103 commented Oct 23, 2019

The issue here is allowing SerializerSpecPlugin.get_value to see the top level instance (or even the whole trail of instances) as well as its own one which it is passed.

DRF does not pass this info through when serializing nested ModelSerializers so its hard to see how we can get hold of it without adapting/extending DRF which is no minor undertaking.

Another approach could be to rewrite the serialization in SSM not to build nested DRF ModelSerializers at all. Also majorish :(.

@pmg103
Copy link
Contributor Author

pmg103 commented Nov 4, 2019

My solution to this has been to have a top-level Plugin prefetch the required sub-data in its modify_queryset() or serialization_spec or both, and then push the required things into the model instance tree in its get_value(). Then user.calculated_value will magically be available in the sub serialization-spec.

I think this solution is OK so the only work to do here is to allow a Plugin to not have a key or return anything in its get_value(), as it doesnt output anything directly into the tree where it is included.

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

No branches or pull requests

1 participant