-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathltree.py
38 lines (26 loc) · 1.02 KB
/
ltree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from django.db import models
class LtreeField(models.TextField):
description = 'ltree'
def __init__(self, *args, **kwargs):
kwargs['editable'] = False
kwargs['null'] = True
kwargs['default'] = None
super(LtreeField, self).__init__(*args, **kwargs)
def db_type(self, connection):
return 'ltree'
class Ancestor(models.Lookup):
lookup_name = 'ancestor'
def as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
params = lhs_params + rhs_params
return '%s @> %s' % (lhs, rhs), params
class Descendant(models.Lookup):
lookup_name = 'descendant'
def as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
params = lhs_params + rhs_params
return '%s <@ %s' % (lhs, rhs), params
LtreeField.register_lookup(Ancestor)
LtreeField.register_lookup(Descendant)