Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/unmanaged_models/models.py
blob: c41bdf59042c552cf7abe9bd7d0de016dcc4450f (plain)
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
"""
Models can have a ``managed`` attribute, which specifies whether the SQL code
is generated for the table on various manage.py operations.
"""

from django.db import models
from django.utils.encoding import python_2_unicode_compatible


#  All of these models are created in the database by Django.


@python_2_unicode_compatible
class A01(models.Model):
    f_a = models.CharField(max_length=10, db_index=True)
    f_b = models.IntegerField()

    class Meta:
        db_table = 'a01'

    def __str__(self):
        return self.f_a


@python_2_unicode_compatible
class B01(models.Model):
    fk_a = models.ForeignKey(A01, models.CASCADE)
    f_a = models.CharField(max_length=10, db_index=True)
    f_b = models.IntegerField()

    class Meta:
        db_table = 'b01'
        # 'managed' is True by default. This tests we can set it explicitly.
        managed = True

    def __str__(self):
        return self.f_a


@python_2_unicode_compatible
class C01(models.Model):
    mm_a = models.ManyToManyField(A01, db_table='d01')
    f_a = models.CharField(max_length=10, db_index=True)
    f_b = models.IntegerField()

    class Meta:
        db_table = 'c01'

    def __str__(self):
        return self.f_a

# All of these models use the same tables as the previous set (they are shadows
# of possibly a subset of the columns). There should be no creation errors,
# since we have told Django they aren't managed by Django.


@python_2_unicode_compatible
class A02(models.Model):
    f_a = models.CharField(max_length=10, db_index=True)

    class Meta:
        db_table = 'a01'
        managed = False

    def __str__(self):
        return self.f_a


@python_2_unicode_compatible
class B02(models.Model):
    class Meta:
        db_table = 'b01'
        managed = False

    fk_a = models.ForeignKey(A02, models.CASCADE)
    f_a = models.CharField(max_length=10, db_index=True)
    f_b = models.IntegerField()

    def __str__(self):
        return self.f_a


# To re-use the many-to-many intermediate table, we need to manually set up
# things up.
@python_2_unicode_compatible
class C02(models.Model):
    mm_a = models.ManyToManyField(A02, through="Intermediate")
    f_a = models.CharField(max_length=10, db_index=True)
    f_b = models.IntegerField()

    class Meta:
        db_table = 'c01'
        managed = False

    def __str__(self):
        return self.f_a


class Intermediate(models.Model):
    a02 = models.ForeignKey(A02, models.CASCADE, db_column="a01_id")
    c02 = models.ForeignKey(C02, models.CASCADE, db_column="c01_id")

    class Meta:
        db_table = 'd01'
        managed = False


# These next models test the creation (or not) of many to many join tables
# between managed and unmanaged models. A join table between two unmanaged
# models shouldn't be automatically created (see #10647).
#

# Firstly, we need some models that will create the tables, purely so that the
# tables are created. This is a test setup, not a requirement for unmanaged
# models.
class Proxy1(models.Model):
    class Meta:
        db_table = "unmanaged_models_proxy1"


class Proxy2(models.Model):
    class Meta:
        db_table = "unmanaged_models_proxy2"


class Unmanaged1(models.Model):
    class Meta:
        managed = False
        db_table = "unmanaged_models_proxy1"


# Unmanged with an m2m to unmanaged: the intermediary table won't be created.
class Unmanaged2(models.Model):
    mm = models.ManyToManyField(Unmanaged1)

    class Meta:
        managed = False
        db_table = "unmanaged_models_proxy2"


# Here's an unmanaged model with an m2m to a managed one; the intermediary
# table *will* be created (unless given a custom `through` as for C02 above).
class Managed1(models.Model):
    mm = models.ManyToManyField(Unmanaged1)