Ticket #23624: models.2.py

File models.2.py, 3.4 KB (added by Ludovico Magnocavallo, 10 years ago)

models.py from the test project that does not trigger the exception

Line 
1import os
2import sys
3
4from types import ModuleType
5
6from django.db import models
7from django.apps import apps, AppConfig
8
9# db schema and traceback at the bottom
10
11"""
12>>> from modeltest.models import get_module
13>>>
14>>> m = get_module()
15>>> m.A(id=1, name='a').save()
16>>> m.B(id=1, name='b').save()
17>>> m.AB(a=m.A.objects.get(id=1), b=m.B.objects.all()[0], extra='ab').save()
18>>> m.AB.objects.select_related('a', 'b') # works in both 1.6 and 1.7
19>>> m.A.objects.get(id=1).b_s.all() # works in 1.6, raises FieldError in 1.7
20>>>
21"""
22
23
24class A(models.Model):
25 id = models.IntegerField(primary_key=True, db_column='a_primary_id')
26 name = models.CharField(max_length=12)
27 b_s = models.ManyToManyField('B', through='AB')
28
29 class Meta:
30 abstract = True
31
32
33class B(models.Model):
34 id = models.IntegerField(primary_key=True, db_column='b_primary_id')
35 name = models.CharField(max_length=12)
36 a_s = models.ManyToManyField('A', through='AB')
37
38 class Meta:
39 abstract = True
40
41class AB(models.Model):
42 a = models.ForeignKey('A', db_column='rel_to_a')
43 b = models.ForeignKey('B', db_column='rel_to_b')
44 extra = models.CharField(max_length=12, blank=True, null=True)
45
46 class Meta:
47 unique_together = (('a', 'b'),)
48 abstract = True
49
50
51def get_module():
52 name = 'modeltest_dynamic.models'
53
54 if name in sys.modules:
55 return sys.modules[name]
56
57 mod = ModuleType(name)
58
59 mod.A = type('A', (A,), {
60 '__module__':mod.__name__,
61 'Meta':type('Meta', (A.Meta, object), {
62 '__module__':mod.__name__,
63 'app_label':'modeltest_dynamic',
64 'managed':False,
65 'db_table':A._meta.db_table
66 })
67 })
68
69 mod.B = type('B', (B,), {
70 '__module__':mod.__name__,
71 'Meta':type('Meta', (B.Meta, object), {
72 '__module__':mod.__name__,
73 'app_label':'modeltest_dynamic',
74 'managed':False,
75 'db_table':B._meta.db_table
76 })
77 })
78
79 mod.AB = type('AB', (AB,), {
80 '__module__':mod.__name__,
81 'Meta':type('Meta', (AB.Meta, object), {
82 '__module__':mod.__name__,
83 'app_label':'modeltest_dynamic',
84 'managed':False,
85 'db_table':AB._meta.db_table
86 })
87 })
88
89 class AppConfigWithPath(AppConfig):
90 path = os.path.dirname(__file__)
91
92
93 app_module = ModuleType('modeltest_dynamic')
94 app_module.models = mod
95
96 sys.modules['modeltest_dynamic'] = app_module
97 sys.modules[name] = mod
98
99
100 app_config = AppConfigWithPath('modeltest_dynamic', app_module)
101
102 apps.app_configs[app_config.label] = app_config
103 all_models = apps.all_models[app_config.label]
104 app_config.import_models(all_models)
105 app_config.ready()
106
107 return mod
108
109
110"""
111CREATE TABLE "modeltest_a" (
112 "a_primary_id" integer NOT NULL PRIMARY KEY,
113 "name" varchar(12) NOT NULL
114);
115CREATE TABLE "modeltest_b" (
116 "b_primary_id" integer NOT NULL PRIMARY KEY,
117 "name" varchar(12) NOT NULL
118);
119CREATE TABLE "modeltest_ab" (
120"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
121"rel_to_a" integer NOT NULL REFERENCES "modeltest_a" ("a_primary_id"),
122"rel_to_b" integer NOT NULL REFERENCES "modeltest_b" ("b_primary_id"),
123"extra" varchar(12),
124UNIQUE ("rel_to_a", "rel_to_b")
125);
126CREATE INDEX "modeltest_ab_4d6cc2fb" ON "modeltest_ab" ("rel_to_a");
127CREATE INDEX "modeltest_ab_2c14050b" ON "modeltest_ab" ("rel_to_b");
128"""
Back to Top