├── README.md ├── .gitignore ├── LICENSE └── ltreefield.py /README.md: -------------------------------------------------------------------------------- 1 | ltreefield 2 | ========== 3 | 4 | Django Field class for PostgreSQL ltree data type. 5 | 6 | Class 7 | ----- 8 | LtreeField 9 | 10 | Lookups 11 | ------- 12 | * aore 13 | * ltree @> ltree 14 | * is left argument an ancestor of right (or equal)? 15 | * dore 16 | * ltree <@ ltree 17 | * is left argument a descendant of right (or equal)? 18 | * match 19 | * ltree ~ lquery 20 | * does ltree match lquery? 21 | 22 | Usage 23 | ----- 24 | ```python 25 | path__aore='Top.Science' # path @> 'Top.Science' 26 | path__dore='Top.Science' # path <@ 'Top.Science' 27 | path__match='*.Astronomy.*' # path ~ '*.Astronomy.*' 28 | ``` 29 | 30 | Reference 31 | --------- 32 | [PostgreSQL: Documentation: 9.3: ltree](http://www.postgresql.org/docs/9.3/static/ltree.html) 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # PyInstaller 26 | # Usually these files are written by a python script from a template 27 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 28 | *.manifest 29 | *.spec 30 | 31 | # Installer logs 32 | pip-log.txt 33 | pip-delete-this-directory.txt 34 | 35 | # Unit test / coverage reports 36 | htmlcov/ 37 | .tox/ 38 | .coverage 39 | .cache 40 | nosetests.xml 41 | coverage.xml 42 | 43 | # Translations 44 | *.mo 45 | *.pot 46 | 47 | # Django stuff: 48 | *.log 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | # PyBuilder 54 | target/ 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 whitglint 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /ltreefield.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.db.models import Lookup 3 | 4 | 5 | class LtreeField(models.CharField): 6 | description = 'ltree (up to %(max_length)s)' 7 | 8 | def __init__(self, *args, **kwargs): 9 | kwargs['max_length'] = 256 10 | super().__init__(*args, **kwargs) 11 | 12 | def db_type(self, connection): 13 | return 'ltree' 14 | 15 | def deconstruct(self): 16 | name, path, args, kwargs = super().deconstruct() 17 | del kwargs['max_length'] 18 | return name, path, args, kwargs 19 | 20 | 21 | class AncestorOrEqual(Lookup): 22 | lookup_name = 'aore' 23 | 24 | def as_sql(self, qn, connection): 25 | lhs, lhs_params = self.process_lhs(qn, connection) 26 | rhs, rhs_params = self.process_rhs(qn, connection) 27 | params = lhs_params + rhs_params 28 | return '%s @> %s' % (lhs, rhs), params 29 | 30 | LtreeField.register_lookup(AncestorOrEqual) 31 | 32 | 33 | class DescendantOrEqual(Lookup): 34 | lookup_name = 'dore' 35 | 36 | def as_sql(self, qn, connection): 37 | lhs, lhs_params = self.process_lhs(qn, connection) 38 | rhs, rhs_params = self.process_rhs(qn, connection) 39 | params = lhs_params + rhs_params 40 | return '%s <@ %s' % (lhs, rhs), params 41 | 42 | LtreeField.register_lookup(DescendantOrEqual) 43 | 44 | 45 | class Match(Lookup): 46 | lookup_name = 'match' 47 | 48 | def as_sql(self, qn, connection): 49 | lhs, lhs_params = self.process_lhs(qn, connection) 50 | rhs, rhs_params = self.process_rhs(qn, connection) 51 | params = lhs_params + rhs_params 52 | return '%s ~ %s' % (lhs, rhs), params 53 | 54 | LtreeField.register_lookup(Match) 55 | --------------------------------------------------------------------------------