#29157: Allow querying for distinct values in JSONField lists
-----------------------------------+------------------------------------
Reporter: Hrishikesh Barman | Owner: (none)
Type: New feature | Status: new
Component: contrib.postgres | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Joey Wilhelm):
* cc: Joey Wilhelm (added)
Comment:
I have a similar use case. Using the following model...
{{{#!python
class Request(models.Model):
request_data = JSONField()
state = models.CharField(max_length=255)
}}}
And JSON data
{{{#!json
{"target": {"pk": 1}}
}}}
I'm able to issue a SQL query like this:
{{{#!sql
SELECT DISTINCT(request_data -> 'target' -> 'pk') FROM myapp_request WHERE
state = 'in_progress';
}}}
I would expect the equivalent ORM code to look like:
{{{#!python
Request.objects.filter(state=Request.STATE_IN_PROGRESS).values('request_data__target__pk').distinct()
}}}
OR
{{{#!python
Request.objects.filter(state=Request.STATE_IN_PROGRESS).annotate(target=F('request_data__target__pk')).values('target').distinct()
}}}
But both produce this error:
{{{#!python
django.core.exceptions.FieldError: Cannot resolve keyword 'target' into
field. Join on 'request_data' not permitted.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29157#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.edd8b06e25eb82e581ec603abc270d71%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.