Ticket #23624: models.py

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

models.py

Line 
1import sys
2
3from types import ModuleType
4
5from django.db import models
6
7# db schema and traceback at the bottom
8
9"""
10>>> from modeltest.models import get_module
11>>>
12>>> m = get_module()
13>>> m.A(id=1, name='a').save()
14>>> m.B(id=1, name='b').save()
15>>> m.AB(a=m.A.objects.get(id=1), b=m.B.objects.all()[0], extra='ab').save()
16>>> m.AB.objects.select_related('a', 'b') # works in both 1.6 and 1.7
17>>> m.A.objects.get(id=1).b_s.all() # works in 1.6, raises FieldError in 1.7
18>>>
19"""
20
21
22class A(models.Model):
23 id = models.IntegerField(primary_key=True, db_column='a_primary_id')
24 name = models.CharField(max_length=12)
25 b_s = models.ManyToManyField('B', through='AB')
26
27 class Meta:
28 abstract = True
29
30
31class B(models.Model):
32 id = models.IntegerField(primary_key=True, db_column='b_primary_id')
33 name = models.CharField(max_length=12)
34 a_s = models.ManyToManyField('A', through='AB')
35
36 class Meta:
37 abstract = True
38
39class AB(models.Model):
40 a = models.ForeignKey('A', db_column='rel_to_a')
41 b = models.ForeignKey('B', db_column='rel_to_b')
42 extra = models.CharField(max_length=12, blank=True, null=True)
43
44 class Meta:
45 unique_together = (('a', 'b'),)
46 abstract = True
47
48
49def get_module():
50 name = 'modeltest_dynamic.models'
51
52 if name in sys.modules:
53 return sys.modules[name]
54
55 mod = ModuleType(name)
56
57 mod.A = type('A', (A,), {
58 '__module__':mod.__name__,
59 'Meta':type('Meta', (A.Meta, object), {
60 '__module__':mod.__name__,
61 'app_label':'modeltest_dynamic',
62 'managed':False,
63 'db_table':A._meta.db_table
64 })
65 })
66
67 mod.B = type('B', (B,), {
68 '__module__':mod.__name__,
69 'Meta':type('Meta', (B.Meta, object), {
70 '__module__':mod.__name__,
71 'app_label':'modeltest_dynamic',
72 'managed':False,
73 'db_table':B._meta.db_table
74 })
75 })
76
77 mod.AB = type('AB', (AB,), {
78 '__module__':mod.__name__,
79 'Meta':type('Meta', (AB.Meta, object), {
80 '__module__':mod.__name__,
81 'app_label':'modeltest_dynamic',
82 'managed':False,
83 'db_table':AB._meta.db_table
84 })
85 })
86
87 sys.modules[name] = mod
88 return mod
89
90
91"""
92CREATE TABLE "modeltest_a" (
93 "a_primary_id" integer NOT NULL PRIMARY KEY,
94 "name" varchar(12) NOT NULL
95);
96CREATE TABLE "modeltest_b" (
97 "b_primary_id" integer NOT NULL PRIMARY KEY,
98 "name" varchar(12) NOT NULL
99);
100CREATE TABLE "modeltest_ab" (
101"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
102"rel_to_a" integer NOT NULL REFERENCES "modeltest_a" ("a_primary_id"),
103"rel_to_b" integer NOT NULL REFERENCES "modeltest_b" ("b_primary_id"),
104"extra" varchar(12),
105UNIQUE ("rel_to_a", "rel_to_b")
106);
107CREATE INDEX "modeltest_ab_4d6cc2fb" ON "modeltest_ab" ("rel_to_a");
108CREATE INDEX "modeltest_ab_2c14050b" ON "modeltest_ab" ("rel_to_b");
109"""
110
111"""
112Django 1.7
113Traceback (most recent call last):
114 File "<console>", line 1, in <module>
115 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/manager.py", line 191, in all
116 return self.get_queryset()
117 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 887, in get_queryset
118 return qs._next_is_sticky().filter(**self.core_filters)
119 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
120 return self._filter_or_exclude(False, *args, **kwargs)
121 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
122 clone.query.add_q(Q(*args, **kwargs))
123 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1287, in add_q
124 clause, require_inner = self._add_q(where_part, self.used_aliases)
125 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1314, in _add_q
126 current_negated=current_negated, connector=connector)
127 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1138, in build_filter
128 lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
129 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1076, in solve_lookup_type
130 _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
131 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1383, in names_to_path
132 self.raise_field_error(opts, name)
133 File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1389, in raise_field_error
134 "Choices are: %s" % (name, ", ".join(available)))
135FieldError: Cannot resolve keyword u'a' into field. Choices are: a_s, id, name
136"""
Back to Top