This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
forked from SelfHacked/django-outer-join
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
90 lines (63 loc) · 2.14 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
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)
b = models.ForeignKey(
# readonly models can have foreign keys to real tables with db_constraint
B0, related_name='+', on_delete=models.CASCADE,
)
val = models.IntegerField()
class A1(AbstractDeleteRecord):
_save_check_fields = ('key',)
key = models.IntegerField(unique=True)
b = models.ForeignKey(
# read-write models should reference the outer-join model
B, related_name='+', on_delete=models.CASCADE,
# and should not have db_constraint
db_constraint=False,
null=True,
)
val = models.IntegerField(null=True)
class A(models.Model):
key = models.IntegerField(primary_key=True)
b = models.ForeignKey(
B, related_name='a_set', on_delete=models.CASCADE,
db_constraint=False,
)
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()()