mariocesar/django-ltree

NLevel transform is not registered as a lookup

ghtyrant opened this issue · 1 comments

Currently, the NLevel transform (functions.py) is not registered as a lookup. This means, that queries like MyModel.objects.filter(path__depth=NLevel(self.path)) won't work:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\query.py", line 892, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\query.py", line 910, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\sql\query.py", line 1290, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\sql\query.py", line 1318, in _add_q
    split_subq=split_subq, simple_col=simple_col,
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\sql\query.py", line 1251, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\sql\query.py", line 1110, in build_lookup
    lhs = self.try_transform(lhs, lookup_name)
  File "C:\Users\user\.virtualenvs\backend-BawPrnSu\lib\site-packages\django\db\models\sql\query.py", line 1151, in try_transform
    "permitted%s" % (name, output_field.__name__, suggestion)
django.core.exceptions.FieldError: Unsupported lookup 'depth' for PathField or join on the field not permitted.

Registering the lookup before running the query fixes this:
PathField.register_lookup(NLevel)

Is there a reason why this is missing?

Django 2.2.6
Python 3.6.5
django-ltree 0.4

@ghtyrant thanks for catching the bug. I added the register and a test for it.

Thanks again !