-
Notifications
You must be signed in to change notification settings - Fork 3
feat: filter submissions by payload content #412
Conversation
Add a dynamic filter to SubmissionViewSet. Any query parameter prefixed by "payload__" will filter submissions based on the contents of Submission.payload. Example: The URL "/submissions?payload__a__b__c=1" will yield the queryset models.Submission.objects.filter(payload__a__b__c=1) and return a list of submissions with a JSON payload like '{"a": {"b": {"c": 1}}}' Note that it is possible to compare not just strings, but numbers, lists and objects as well.
@@ -310,6 +312,16 @@ class SubmissionViewSet(CustomViewSet): | |||
serializer_class = serializers.SubmissionSerializer | |||
filter_class = filters.SubmissionFilter | |||
|
|||
def get_queryset(self): | |||
queryset = models.Submission.objects.all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will remove previous filters executed by the SubmissionFilter
class like the project
filter 😱
The correct line is queryset = self.queryset
😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh thanks, I thought this ran before the other filters. I'll take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you are right, but imagine that the default queryset
is something like this:
queryset = Project.objects \
.prefetch_related('xforms', 'xforms__media_files') \
.order_by('name')
or
queryset = models.Project \
.objects \
.values('id', 'name', 'created') \
.annotate(
first_submission=Min('submissions__created'),
last_submission=Max('submissions__created'),
submissions_count=Count('submissions__id', distinct=True),
entities_count=Count('submissions__entities__id', distinct=True),
)
🤷♀️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, agreed it's much clearer to use self.queryset
. Fixing.
4061308
to
96bba62
Compare
Add a dynamic filter to SubmissionViewSet and EntityViewSet.
Any query parameter prefixed by "payload__" will filter submissions
based on the contents of e.g. Submission.payload.
Example:
The URL "/submissions?payload__a__b__c=1" will yield
the queryset
models.Submission.objects.filter(payload__a__b__c=1)
and return a list of submissions with a JSON payload like
'{"a": {"b": {"c": 1}}}'
Note that it is possible to compare not just strings, but
numbers, lists and objects as well.