@m0ody
backend dev (python, django, postgresql, celery)

Django.contrib.postgres HStoreField — вложенный подзапрос, баг?

Приветствую коллеги. Есть две модели:
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)

При выполнении следующего подзапроса:
print B.objects.filter(a__in=A.objects.filter(hstore__field=1))

Получаю следующее исключение:
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)

Это баг?
  • Вопрос задан
  • 417 просмотров
Решения вопроса 1
@m0ody Автор вопроса
backend dev (python, django, postgresql, celery)
Комментировать
Пригласить эксперта
Ответы на вопрос 1
vinograd19
@vinograd19
По всей видимости, действительно баг.

Можете пользоваться таким костылем
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)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы