M
M
m0ody2015-05-02 21:51:53
Django
m0ody, 2015-05-02 21:51:53

Django.contrib.postgres HStoreField - nested subquery, bug?

Greetings colleagues. There are two models:

from django.db import models
from django.contrib.postgres.fields import HStoreField


class A(Model):
    test = models.CharField(max_length=100)
    hstore = HStoreField()


class B(Model):
    a = models.ForeignKey(A)

When executing the following subquery:
print B.objects.filter(a__in=A.objects.filter(hstore__field=1))

I am getting the following exception:
Traceback (most recent call last):
  File "/vagrant/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/vagrant/st/apps.py", line 18, in ready
    print str(B.objects.filter(a__in=A.objects.filter(hstore__field=1)).query)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 679, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 697, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1301, in add_q
    clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1328, in _add_q
    current_negated=current_negated, connector=connector, allow_joins=allow_joins)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1150, in build_filter
    value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1007, in prepare_lookup_value
    value.query.bump_prefix(self)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 868, in bump_prefix
    self.change_aliases(change_map)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 792, in change_aliases
    self.where.relabel_aliases(change_map)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py", line 287, in relabel_aliases
    self.children[pos] = child.relabeled_clone(change_map)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 178, in relabeled_clone
    new.lhs = new.lhs.relabeled_clone(relabels)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 76, in relabeled_clone
    return self.__class__(self.lhs.relabeled_clone(relabels))
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/postgres/fields/hstore.py", line 76, in __init__
    super(KeyTransform, self).__init__(*args, **kwargs)
TypeError: __init__() takes exactly 3 arguments (1 given)

Is this a bug?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
m0ody, 2015-05-05
@m0ody

Ticket: https://code.djangoproject.com/ticket/24744
Patch: https://github.com/django/django/pull/4766

V
vinograd19, 2015-05-04
@vinograd19

Apparently, really a bug.
You can use this crutch

a_qs = A.objects.filter(hstore__field=1)
a_id_list = list(a_qs.values_list('id', flat=True))
b_qs = B.objects.filter(a_id__in=a_id_list)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question