[Django] #30711: Add HStoreF for F object like querying on HStoreField.

30 views
Skip to first unread message

Django

unread,
Aug 15, 2019, 8:21:59 AM8/15/19
to django-...@googlegroups.com
#30711: Add HStoreF for F object like querying on HStoreField.
-------------------------------------+-------------------------------------
Reporter: tiptop96 | Owner: (none)
Type: New | Status: new
feature |
Component: | Version: 2.2
contrib.postgres |
Severity: Normal | Keywords: HStoreField F
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When using F objects for key lookups on HStoreFields it never digs down to
get the keys but unexpectedly returns the entire object.

Lets say we have this model:
{{{
from django.contrib.postgres.fields import HStoreField
from django.db import models

class Person(models.Model):
name = models.CharField(max_length=256)
attributes = HStoreField()
}}}

We then populate it.
{{{
Person.objects.create(name="James", attributes={"age": "30", "height":
"190"})
}}}

Here comes the thing that bothers me. If we try to access either of these
keys the entire attributes cell will be returned.
{{{
p = Person.objects.annotate(length=F("attributes__height"))
p[0].height
>>> {"age": "30", "height": "190"}
}}}

While the expected result would be an error or the value of the height
field, it's not.

I propose either making F objects support these fields or creating a new
type to handle this, a workaround that I'm playing around with myself.

{{{
from django.contrib.postgres.fields.jsonb import KeyTransformFactory

class HStoreF(F):
def resolve_expression(self, query=None, allow_joins=True, reuse=None,
summarize=False, for_save=False):
rhs = super().resolve_expression(query, allow_joins, reuse,
summarize, for_save)
field_list = self.name.split("__")
if field_list[-1] == rhs.target.name:
raise LookupError(
"HStoreF requires a key lookup in order to avoid unexpected
behavior. "
"Please append '__somekey' to '{}'."
.format("__".join(field_list)))
return KeyTransformFactory(field_list[-1])(rhs)

}}}

An issue with this is that updating models with it still wont work. As an
error is raised in {{{Query.resolve_ref()}}} due to the fact that it
interprets the {{{"__"}}} as an attempted join. This could be solved by
using a different lookup separator for keys (maybe relevant for JSONField
to?), but I was unable to successfully implement this.

Similar issue with JSONField: https://code.djangoproject.com/ticket/29769

--
Ticket URL: <https://code.djangoproject.com/ticket/30711>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 15, 2019, 9:17:40 AM8/15/19
to django-...@googlegroups.com
#30711: Add HStoreF for F object like querying on HStoreField.
----------------------------------+--------------------------------------
Reporter: Gustav Eiman | Owner: (none)
Type: New feature | Status: new
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by felixxm):

* version: 2.2 => master


Comment:

Thanks for the report, I don't think that we should support this by adding
a custom expression because currently you can always use
`django.contrib.postgres.fields.hstore.KeyTransform`, e.g.
{{{
Person.objects.annotate(height=KeyTransform('height', 'attributes'))
}}}
I don't think that we need to add anything new. IMO, documentating
`django.contrib.postgres.fields.hstore.KeyTransform` should be enough.

It's more complicated for JSONField because nesting multiple
`KeyTransform()` is not so handy.

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:1>

Django

unread,
Aug 15, 2019, 9:40:55 AM8/15/19
to django-...@googlegroups.com
#30711: Add HStoreF for F object like querying on HStoreField.
----------------------------------+--------------------------------------
Reporter: Gustav Eiman | Owner: (none)
Type: New feature | Status: new
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Gustav Eiman):

Replying to [comment:1 felixxm]:


> Thanks for the report, I don't think that we should support this by
adding a custom expression because currently you can always use
`django.contrib.postgres.fields.hstore.KeyTransform`, e.g.
> {{{
> Person.objects.annotate(height=KeyTransform('height', 'attributes'))
> }}}
> I don't think that we need to add anything new. IMO, documentating
`django.contrib.postgres.fields.hstore.KeyTransform` should be enough.
>
> It's more complicated for JSONField because nesting multiple
`KeyTransform()` is not so handy.

Thank you! I agree, I had no idea this was available.

Being a first time poster, what do I do now? Should I close this ticket?

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:2>

Django

unread,
Aug 15, 2019, 9:59:56 AM8/15/19
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
--------------------------------------+------------------------------------
Reporter: Gustav Eiman | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by felixxm):

* component: contrib.postgres => Documentation
* owner: (none) => nobody
* type: New feature => Cleanup/optimization
* stage: Unreviewed => Accepted


Old description:

New description:

Document `django.contrib.postgres.fields.hstore.KeyTransform` in the
[https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#hstorefield
HStoreField documentation] that can be used on the right hand side of a
filter or an annotation.

--

Comment:

I changed ticket description, thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:3>

Django

unread,
Aug 23, 2019, 8:40:49 AM8/23/19
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
--------------------------------------+------------------------------------
Reporter: Gustav Eiman | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by tapaswenipathak):

Hello folks: Can I take the ticket?

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:4>

Django

unread,
Aug 23, 2019, 8:49:35 AM8/23/19
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner:
Type: | tapaswenipathak
Cleanup/optimization | Status: assigned

Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Gustav Eiman):

* owner: nobody => tapaswenipathak
* status: new => assigned


Comment:

Replying to [comment:4 tapaswenipathak]:


> Hello folks: Can I take the ticket?

That would be great! I'll assign it to you.

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:5>

Django

unread,
Jul 23, 2021, 4:30:28 AM7/23/21
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
--------------------------------------+------------------------------------
Reporter: Gustav Eiman | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* owner: Tapasweni Pathak => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:6>

Django

unread,
Jun 15, 2022, 3:28:12 AM6/15/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alokik Roy):

* owner: (none) => Alokik Roy


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:7>

Django

unread,
Jul 11, 2022, 7:19:40 AM7/11/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Alokik Roy):

PR [https://github.com/django/django/pull/15833]

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:8>

Django

unread,
Jul 13, 2022, 4:57:47 AM7/13/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: HStoreField F | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:9>

Django

unread,
Jul 13, 2022, 5:35:22 AM7/13/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed

Keywords: HStoreField F | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"0acaea1329e1b31d38dff2b58fc9088814981e0d" 0acaea13]:
{{{
#!CommitTicketReference repository=""
revision="0acaea1329e1b31d38dff2b58fc9088814981e0d"
[4.1.x] Fixed #30711 -- Doc'd
django.contrib.postgres.fields.hstore.KeyTransform().

Backport of 7faf25d682b8e8f4fd2006eb7dfc71ed2a2193b7 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:10>

Django

unread,
Jul 13, 2022, 5:35:22 AM7/13/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: HStoreField F | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"7faf25d682b8e8f4fd2006eb7dfc71ed2a2193b7" 7faf25d]:
{{{
#!CommitTicketReference repository=""
revision="7faf25d682b8e8f4fd2006eb7dfc71ed2a2193b7"


Fixed #30711 -- Doc'd
django.contrib.postgres.fields.hstore.KeyTransform().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:11>

Django

unread,
Aug 31, 2022, 4:12:57 PM8/31/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: HStoreField F | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"cb06f5ef8c80fc6a610db0fc36fb9dc7c625335a" cb06f5ef]:
{{{
#!CommitTicketReference repository=""
revision="cb06f5ef8c80fc6a610db0fc36fb9dc7c625335a"
Reverted "Fixed #30711 -- Doc'd
django.contrib.postgres.fields.hstore.KeyTransform()."

This reverts commit 7faf25d682b8e8f4fd2006eb7dfc71ed2a2193b7. The same
can be achieved with F() so there is no need to expose an extra API.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:12>

Django

unread,
Aug 31, 2022, 4:14:36 PM8/31/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: HStoreField F | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"02876534aba63755e7418ee03a691ef6dcdb4416" 02876534]:
{{{
#!CommitTicketReference repository=""
revision="02876534aba63755e7418ee03a691ef6dcdb4416"
[4.1.x] Reverted "Fixed #30711 -- Doc'd
django.contrib.postgres.fields.hstore.KeyTransform()."

This reverts commit 7faf25d682b8e8f4fd2006eb7dfc71ed2a2193b7. The same
can be achieved with F() so there is no need to expose an extra API.

Backport of cb06f5ef8c80fc6a610db0fc36fb9dc7c625335a from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:13>

Django

unread,
Aug 31, 2022, 4:15:30 PM8/31/22
to django-...@googlegroups.com
#30711: Document django.contrib.postgres.fields.hstore.KeyTransform.
-------------------------------------+-------------------------------------
Reporter: Gustav Eiman | Owner: Alokik
Type: | Roy
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: HStoreField F | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* resolution: fixed => wontfix
* stage: Ready for checkin => Unreviewed


Comment:

The same can be achieved with `F()` so there is no need to expose an extra
API.

--
Ticket URL: <https://code.djangoproject.com/ticket/30711#comment:14>

Reply all
Reply to author
Forward
0 new messages