This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
148 lines (107 loc) · 3.39 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from django.db import models
from outer_join import WritableOuterJoin
from outer_join.extra.models import AbstractDeleteRecord
from outer_join.extra.queryset import exclude_exact
class B0(models.Model):
key = models.IntegerField(unique=True)
val = models.IntegerField()
class B1(AbstractDeleteRecord):
_save_check_fields = ('key',)
key = models.IntegerField(unique=True)
val = models.IntegerField(null=True)
class B(models.Model):
key = models.IntegerField(primary_key=True)
val = models.IntegerField(null=True)
is_deleted = models.BooleanField(null=True)
class Meta:
managed = False
base_manager_name = 'base_objects'
outer_join = WritableOuterJoin(
B1, B0,
on='key',
)
objects = outer_join.get_manager(
filter_initial_queryset=exclude_exact('is_deleted', True),
)()
base_objects = outer_join.get_manager()()
class A0(models.Model):
key = models.IntegerField(unique=True)
# this M2M field is optional because it's not an actual table column
# however since A0, B0 and Through0 are all readonly,
# we may put this field here for easier access
b_set = models.ManyToManyField(
B0, related_name='a_set', through='Through0',
)
val = models.IntegerField()
class A1(AbstractDeleteRecord):
_save_check_fields = ('key',)
key = models.IntegerField(unique=True)
val = models.IntegerField(null=True)
class A(models.Model):
key = models.IntegerField(primary_key=True)
b_set = models.ManyToManyField(
B, related_name='a_set', through='Through',
)
val = models.IntegerField()
is_deleted = models.BooleanField(null=True)
class Meta:
managed = False
base_manager_name = 'base_objects'
outer_join = WritableOuterJoin(
A1, A0,
on='key',
)
objects = outer_join.get_manager(
filter_initial_queryset=exclude_exact('is_deleted', True),
)()
base_objects = outer_join.get_manager()()
class Through0(models.Model):
a = models.ForeignKey(
A0, related_name='+', on_delete=models.CASCADE,
)
b = models.ForeignKey(
B0, related_name='+', on_delete=models.CASCADE,
)
class Meta:
unique_together = (
('a', 'b'),
)
class Through1(AbstractDeleteRecord):
a = models.ForeignKey(
A, related_name='+', on_delete=models.CASCADE,
db_constraint=False,
)
b = models.ForeignKey(
B, related_name='+', on_delete=models.CASCADE,
db_constraint=False,
)
class Meta:
unique_together = (
('a', 'b'),
)
class Through(models.Model):
_save_check_fields = ('a', 'b',)
a = models.ForeignKey(
A, related_name='+', on_delete=models.CASCADE,
db_constraint=False,
)
b = models.ForeignKey(
B, related_name='+', on_delete=models.CASCADE,
db_constraint=False,
)
is_deleted = models.BooleanField(null=True)
class Meta:
unique_together = (
('a', 'b'),
)
managed = False
base_manager_name = 'base_objects'
outer_join = WritableOuterJoin(
Through1, Through0,
on=['a', 'b'],
)
primary_key = outer_join.get_primary_key()()
objects = outer_join.get_manager(
filter_initial_queryset=exclude_exact('is_deleted', True),
)()
base_objects = outer_join.get_manager()()