| 1 | # models.py
|
|---|
| 2 |
|
|---|
| 3 | from django.db import models
|
|---|
| 4 |
|
|---|
| 5 | class ModelOne(models.Model):
|
|---|
| 6 | field = models.CharField(max_length=10)
|
|---|
| 7 |
|
|---|
| 8 | class ModelTwo(models.Model):
|
|---|
| 9 | model_one = models.ForeignKey(ModelOne)
|
|---|
| 10 |
|
|---|
| 11 | class ModelThree(models.Model):
|
|---|
| 12 | model_one = models.ForeignKey(ModelOne)
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | # Then, in ./manage.py shell (the name of my test app is "exclude"):
|
|---|
| 16 |
|
|---|
| 17 | In[0]: from exclude.models import ModelTwo
|
|---|
| 18 |
|
|---|
| 19 | In [1]: ModelTwo.objects.filter(model_one__modelthree__id=1)
|
|---|
| 20 | Out[1]: []
|
|---|
| 21 |
|
|---|
| 22 | In [2]: ModelTwo.objects.exclude(model_one__modelthree__id=1)
|
|---|
| 23 | Out[2]: ERROR: Internal Python error in the inspect module.
|
|---|
| 24 | Below is the traceback from this internal error.
|
|---|
| 25 |
|
|---|
| 26 | Traceback (most recent call last):
|
|---|
| 27 | File "/usr/lib/python2.5/site-packages/IPython/ultraTB.py", line 635, in text
|
|---|
| 28 | locals,formatvalue=var_repr))
|
|---|
| 29 | File "/usr/lib/python2.5/inspect.py", line 812, in formatargvalues
|
|---|
| 30 | specs.append(strseq(args[i], convert, join))
|
|---|
| 31 | File "/usr/lib/python2.5/inspect.py", line 767, in strseq
|
|---|
| 32 | return convert(object)
|
|---|
| 33 | File "/usr/lib/python2.5/inspect.py", line 809, in convert
|
|---|
| 34 | return formatarg(name) + formatvalue(locals[name])
|
|---|
| 35 | KeyError: 'connection'
|
|---|
| 36 |
|
|---|
| 37 | IPython's exception reporting continues...
|
|---|
| 38 |
|
|---|
| 39 | ---------------------------------------------------------------------------
|
|---|
| 40 | OperationalError Traceback (most recent call last)
|
|---|
| 41 |
|
|---|
| 42 | /home/brent/django/testing/<ipython console> in <module>()
|
|---|
| 43 |
|
|---|
| 44 | /usr/lib/python2.5/site-packages/IPython/Prompts.py in __call__(self, arg)
|
|---|
| 45 | 533
|
|---|
| 46 | 534 # and now call a possibly user-defined print mechanism
|
|---|
| 47 | --> 535 manipulated_val = self.display(arg)
|
|---|
| 48 | 536
|
|---|
| 49 | 537 # user display hooks can change the variable to be stored in
|
|---|
| 50 |
|
|---|
| 51 | /usr/lib/python2.5/site-packages/IPython/Prompts.py in _display(self, arg)
|
|---|
| 52 | 559 return IPython.generics.result_display(arg)
|
|---|
| 53 | 560 except TryNext:
|
|---|
| 54 | --> 561 return self.shell.hooks.result_display(arg)
|
|---|
| 55 | 562
|
|---|
| 56 | 563 # Assign the default display method:
|
|---|
| 57 |
|
|---|
| 58 | /usr/lib/python2.5/site-packages/IPython/hooks.py in __call__(self, *args, **kw)
|
|---|
| 59 | 132 #print "prio",prio,"cmd",cmd #dbg
|
|---|
| 60 | 133 try:
|
|---|
| 61 | --> 134 ret = cmd(*args, **kw)
|
|---|
| 62 | 135 return ret
|
|---|
| 63 | 136 except ipapi.TryNext, exc:
|
|---|
| 64 |
|
|---|
| 65 | /usr/lib/python2.5/site-packages/IPython/hooks.py in result_display(self, arg)
|
|---|
| 66 | 160
|
|---|
| 67 | 161 if self.rc.pprint:
|
|---|
| 68 | --> 162 out = pformat(arg)
|
|---|
| 69 | 163 if '\n' in out:
|
|---|
| 70 | 164 # So that multi-line strings line up with the left column of
|
|---|
| 71 |
|
|---|
| 72 | /usr/lib/python2.5/pprint.py in pformat(self, object)
|
|---|
| 73 | 109 def pformat(self, object):
|
|---|
| 74 | 110 sio = _StringIO()
|
|---|
| 75 | --> 111 self._format(object, sio, 0, 0, {}, 0)
|
|---|
| 76 | 112 return sio.getvalue()
|
|---|
| 77 | 113
|
|---|
| 78 |
|
|---|
| 79 | /usr/lib/python2.5/pprint.py in _format(self, object, stream, indent, allowance, context, level)
|
|---|
| 80 | 127 self._readable = False
|
|---|
| 81 | 128 return
|
|---|
| 82 | --> 129 rep = self._repr(object, context, level - 1)
|
|---|
| 83 | 130 typ = _type(object)
|
|---|
| 84 | 131 sepLines = _len(rep) > (self._width - 1 - indent - allowance)
|
|---|
| 85 |
|
|---|
| 86 | /usr/lib/python2.5/pprint.py in _repr(self, object, context, level)
|
|---|
| 87 | 193 def _repr(self, object, context, level):
|
|---|
| 88 | 194 repr, readable, recursive = self.format(object, context.copy(),
|
|---|
| 89 | --> 195 self._depth, level)
|
|---|
| 90 | 196 if not readable:
|
|---|
| 91 | 197 self._readable = False
|
|---|
| 92 |
|
|---|
| 93 | /usr/lib/python2.5/pprint.py in format(self, object, context, maxlevels, level)
|
|---|
| 94 | 205 and whether the object represents a recursive construct.
|
|---|
| 95 | 206 """
|
|---|
| 96 | --> 207 return _safe_repr(object, context, maxlevels, level)
|
|---|
| 97 | 208
|
|---|
| 98 | 209
|
|---|
| 99 |
|
|---|
| 100 | /usr/lib/python2.5/pprint.py in _safe_repr(object, context, maxlevels, level)
|
|---|
| 101 | 290 return format % _commajoin(components), readable, recursive
|
|---|
| 102 | 291
|
|---|
| 103 | --> 292 rep = repr(object)
|
|---|
| 104 | 293 return rep, (rep and not rep.startswith('<')), False
|
|---|
| 105 | 294
|
|---|
| 106 |
|
|---|
| 107 | /usr/lib/python2.5/site-packages/django/db/models/query.py in __repr__(self)
|
|---|
| 108 | 142
|
|---|
| 109 | 143 def __repr__(self):
|
|---|
| 110 | --> 144 return repr(list(self))
|
|---|
| 111 | 145
|
|---|
| 112 | 146 def __len__(self):
|
|---|
| 113 |
|
|---|
| 114 | /usr/lib/python2.5/site-packages/django/db/models/query.py in __len__(self)
|
|---|
| 115 | 154 self._result_cache = list(self.iterator())
|
|---|
| 116 | 155 elif self._iter:
|
|---|
| 117 | --> 156 self._result_cache.extend(list(self._iter))
|
|---|
| 118 | 157 return len(self._result_cache)
|
|---|
| 119 | 158
|
|---|
| 120 |
|
|---|
| 121 | /usr/lib/python2.5/site-packages/django/db/models/query.py in iterator(self)
|
|---|
| 122 | 267 extra_select = self.query.extra_select.keys()
|
|---|
| 123 | 268 index_start = len(extra_select)
|
|---|
| 124 | --> 269 for row in self.query.results_iter():
|
|---|
| 125 | 270 if fill_cache:
|
|---|
| 126 | 271 obj, _ = get_cached_row(self.model, row, index_start,
|
|---|
| 127 |
|
|---|
| 128 | /usr/lib/python2.5/site-packages/django/db/models/sql/query.py in results_iter(self)
|
|---|
| 129 | 204 resolve_columns = hasattr(self, 'resolve_columns')
|
|---|
| 130 | 205 fields = None
|
|---|
| 131 | --> 206 for rows in self.execute_sql(MULTI):
|
|---|
| 132 | 207 for row in rows:
|
|---|
| 133 | 208 if resolve_columns:
|
|---|
| 134 |
|
|---|
| 135 | /usr/lib/python2.5/site-packages/django/db/models/sql/query.py in execute_sql(self, result_type)
|
|---|
| 136 | 1721
|
|---|
| 137 | 1722 cursor = self.connection.cursor()
|
|---|
| 138 | -> 1723 cursor.execute(sql, params)
|
|---|
| 139 | 1724
|
|---|
| 140 | 1725 if not result_type:
|
|---|
| 141 |
|
|---|
| 142 | /usr/lib/python2.5/site-packages/django/db/backends/mysql/base.py in execute(self, query, args)
|
|---|
| 143 | 81 def execute(self, query, args=None):
|
|---|
| 144 | 82 try:
|
|---|
| 145 | ---> 83 return self.cursor.execute(query, args)
|
|---|
| 146 | 84 except Database.OperationalError, e:
|
|---|
| 147 | 85 # Map some error codes to IntegrityError, since they seem to be
|
|---|
| 148 |
|
|---|
| 149 | /usr/lib/python2.5/site-packages/MySQLdb/cursors.py in execute(self, query, args)
|
|---|
| 150 | 164 del tb
|
|---|
| 151 | 165 self.messages.append((exc, value))
|
|---|
| 152 | --> 166 self.errorhandler(self, exc, value)
|
|---|
| 153 | 167 self._executed = query
|
|---|
| 154 | 168 if not self._defer_warnings: self._warning_check()
|
|---|
| 155 |
|
|---|
| 156 | /usr/lib/python2.5/site-packages/MySQLdb/connections.py in defaulterrorhandler(***failed resolving arguments***)
|
|---|
| 157 | 33 del cursor
|
|---|
| 158 | 34 del connection
|
|---|
| 159 | ---> 35 raise errorclass, errorvalue
|
|---|
| 160 | 36
|
|---|
| 161 | 37
|
|---|
| 162 |
|
|---|
| 163 | OperationalError: (1054, "Unknown column 'U1.id' in 'on clause'")
|
|---|
| 164 |
|
|---|
| 165 | # Here is the query that django generates. As you can see, it tried to use the U1 alias for a table, which it did not create.
|
|---|
| 166 |
|
|---|
| 167 | 'SELECT `exclude_modeltwo`.`id`, `exclude_modeltwo`.`model_one_id`
|
|---|
| 168 | FROM `exclude_modeltwo`
|
|---|
| 169 | WHERE NOT (`exclude_modeltwo`.`model_one_id`
|
|---|
| 170 | IN (SELECT U2.`model_one_id`
|
|---|
| 171 | FROM `exclude_modeltwo` U0
|
|---|
| 172 | INNER JOIN `exclude_modelthree` U2 ON (U1.`id` = U2.`model_one_id`)
|
|---|
| 173 | WHERE U2.`id` = %s ))'
|
|---|