|
1 """ |
|
2 Models can have a ``managed`` attribute, which specifies whether the SQL code |
|
3 is generated for the table on various manage.py operations. |
|
4 """ |
|
5 |
|
6 from django.db import models |
|
7 |
|
8 # All of these models are creatd in the database by Django. |
|
9 |
|
10 class A01(models.Model): |
|
11 f_a = models.CharField(max_length=10, db_index=True) |
|
12 f_b = models.IntegerField() |
|
13 |
|
14 class Meta: |
|
15 db_table = 'A01' |
|
16 |
|
17 def __unicode__(self): |
|
18 return self.f_a |
|
19 |
|
20 class B01(models.Model): |
|
21 fk_a = models.ForeignKey(A01) |
|
22 f_a = models.CharField(max_length=10, db_index=True) |
|
23 f_b = models.IntegerField() |
|
24 |
|
25 class Meta: |
|
26 db_table = 'B01' |
|
27 # 'managed' is True by default. This tests we can set it explicitly. |
|
28 managed = True |
|
29 |
|
30 def __unicode__(self): |
|
31 return self.f_a |
|
32 |
|
33 class C01(models.Model): |
|
34 mm_a = models.ManyToManyField(A01, db_table='D01') |
|
35 f_a = models.CharField(max_length=10, db_index=True) |
|
36 f_b = models.IntegerField() |
|
37 |
|
38 class Meta: |
|
39 db_table = 'C01' |
|
40 |
|
41 def __unicode__(self): |
|
42 return self.f_a |
|
43 |
|
44 # All of these models use the same tables as the previous set (they are shadows |
|
45 # of possibly a subset of the columns). There should be no creation errors, |
|
46 # since we have told Django they aren't managed by Django. |
|
47 |
|
48 class A02(models.Model): |
|
49 f_a = models.CharField(max_length=10, db_index=True) |
|
50 |
|
51 class Meta: |
|
52 db_table = 'A01' |
|
53 managed = False |
|
54 |
|
55 def __unicode__(self): |
|
56 return self.f_a |
|
57 |
|
58 class B02(models.Model): |
|
59 class Meta: |
|
60 db_table = 'B01' |
|
61 managed = False |
|
62 |
|
63 fk_a = models.ForeignKey(A02) |
|
64 f_a = models.CharField(max_length=10, db_index=True) |
|
65 f_b = models.IntegerField() |
|
66 |
|
67 def __unicode__(self): |
|
68 return self.f_a |
|
69 |
|
70 # To re-use the many-to-many intermediate table, we need to manually set up |
|
71 # things up. |
|
72 class C02(models.Model): |
|
73 mm_a = models.ManyToManyField(A02, through="Intermediate") |
|
74 f_a = models.CharField(max_length=10, db_index=True) |
|
75 f_b = models.IntegerField() |
|
76 |
|
77 class Meta: |
|
78 db_table = 'C01' |
|
79 managed = False |
|
80 |
|
81 def __unicode__(self): |
|
82 return self.f_a |
|
83 |
|
84 class Intermediate(models.Model): |
|
85 a02 = models.ForeignKey(A02, db_column="a01_id") |
|
86 c02 = models.ForeignKey(C02, db_column="c01_id") |
|
87 |
|
88 class Meta: |
|
89 db_table = 'D01' |
|
90 managed = False |
|
91 |
|
92 # |
|
93 # These next models test the creation (or not) of many to many join tables |
|
94 # between managed and unmanaged models. A join table between two unmanaged |
|
95 # models shouldn't be automatically created (see #10647). |
|
96 # |
|
97 |
|
98 # Firstly, we need some models that will create the tables, purely so that the |
|
99 # tables are created. This is a test setup, not a requirement for unmanaged |
|
100 # models. |
|
101 class Proxy1(models.Model): |
|
102 class Meta: |
|
103 db_table = "unmanaged_models_proxy1" |
|
104 |
|
105 class Proxy2(models.Model): |
|
106 class Meta: |
|
107 db_table = "unmanaged_models_proxy2" |
|
108 |
|
109 class Unmanaged1(models.Model): |
|
110 class Meta: |
|
111 managed = False |
|
112 db_table = "unmanaged_models_proxy1" |
|
113 |
|
114 # Unmanged with an m2m to unmanaged: the intermediary table won't be created. |
|
115 class Unmanaged2(models.Model): |
|
116 mm = models.ManyToManyField(Unmanaged1) |
|
117 |
|
118 class Meta: |
|
119 managed = False |
|
120 db_table = "unmanaged_models_proxy2" |
|
121 |
|
122 # Here's an unmanaged model with an m2m to a managed one; the intermediary |
|
123 # table *will* be created (unless given a custom `through` as for C02 above). |
|
124 class Managed1(models.Model): |
|
125 mm = models.ManyToManyField(Unmanaged1) |