-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
How to annotate the historical model to the QuerySet #1321
Comments
There are already some discussions in #407 – However, I found none of them satisfying. There should be an easy way to annotate the history in a View's queryset, especially when this needs to be done on a list. In our case, we have an API with a ListView that returns pages of 100 objects, and for each history entry we need to resolve a foreign key (which is stored as the object ID in the history). The only point where we have easy access to the historical queryset is the serializer field's My suggestion would be to provide a Mixin for Views which can include the historical queryset into the View's normal queryset, where users can then annotate the queryset as usual, and where data is given to the history field as actual data. Maybe, in addition to this, also provide a specialised field type. |
@gabn88 to answer your question: our solution to your problem was to write a custom field type to use in our serializer, and annotate the queryset in that field's |
I'm using this (https://schinckel.net/2019/07/30/subquery-and-subclasses/) and then annotate the required fields as json. That works fine for read-only fields. Actual code used:
|
Problem Statement
We want to return on the API the a list of instances (a QuerySet), where the historical_model should be annotated and returned with only one extra query (instead of N extra queries).
Describe the solution you'd like
I tried this with a SubQuery inside an annotate:
However, because the id of the historicalModel is not a 'real' ForeignKey, the next call to prefetch the instances fails:
Describe alternatives you've considered
If there are ways to do this I'd love to learn.
The text was updated successfully, but these errors were encountered: