forked from dimagi/django-cte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
80 lines (53 loc) · 1.53 KB
/
models.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import absolute_import
from __future__ import unicode_literals
from django.db.models import (
CASCADE,
Model,
AutoField,
CharField,
ForeignKey,
IntegerField,
TextField,
)
from django_cte import CTEManager, CTEQuerySet
class LT40QuerySet(CTEQuerySet):
def lt40(self):
return self.filter(amount__lt=40)
class LT30QuerySet(CTEQuerySet):
def lt30(self):
return self.filter(amount__lt=30)
class LT25QuerySet(CTEQuerySet):
def lt25(self):
return self.filter(amount__lt=25)
class LTManager(CTEManager):
pass
class Region(Model):
objects = CTEManager()
name = TextField(primary_key=True)
parent = ForeignKey("self", null=True, on_delete=CASCADE)
class Order(Model):
objects = CTEManager()
id = AutoField(primary_key=True)
region = ForeignKey(Region, on_delete=CASCADE)
amount = IntegerField(default=0)
class OrderFromLT40(Order):
class Meta:
proxy = True
objects = CTEManager.from_queryset(LT40QuerySet)()
class OrderLT40AsManager(Order):
class Meta:
proxy = True
objects = LT40QuerySet.as_manager()
class OrderCustomManagerNQuery(Order):
class Meta:
proxy = True
objects = LTManager.from_queryset(LT25QuerySet)()
class OrderCustomManager(Order):
class Meta:
proxy = True
objects = LTManager()
class KeyPair(Model):
objects = CTEManager()
key = CharField(max_length=32)
value = IntegerField(default=0)
parent = ForeignKey("self", null=True, on_delete=CASCADE)