Opened 19 years ago

Last modified 11 years ago

#1796 closed defect

[patch] "Cannot resolve keyword ___ into field" error when working with ManyToMany relation — at Version 11

Reported by: anonymous Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: dev
Severity: critical Keywords:
Cc: simon@…, gabor@…, Maniac@…, freakboy3742@…, tom@…, rbreathe@…, pawel.kowalak@…, v.oostveen@…, mir@…, rudolph.froger@…, jhmsmits@…, bon_jovina@…, virtel@…, django@…, deadwisdom@…, yannvr@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adrian Holovaty)

Any attempt to get the related objects from a many to many relation using all() fails. For example the line

a1.primary_categories.all()

in tests/modeltests/m2m_multiple fails with the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 88, in __
repr__
    return repr(self._get_data())
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 378, in _
get_data
    self._result_cache = list(self.iterator())
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 159, in i
terator
    select, sql, params = self._get_sql_clause()
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 392, in _
get_sql_clause
    tables2, joins2, where2, params2 = self._filters.get_sql(opts)
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 523, in g
et_sql
    tables2, joins2, where2, params2 = val.get_sql(opts)
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 572, in g
et_sql
    return parse_lookup(self.kwargs.items(), opts)
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 677, in p
arse_lookup
    tables2, joins2, where2, params2 = lookup_inner(path, clause, value, opts, o
pts.db_table, None)
  File "C:\Python24\lib\site-packages\django\db\models\query.py", line 780, in l
ookup_inner
    raise TypeError, "Cannot resolve keyword '%s' into field" % name
TypeError: Cannot resolve keyword 'primary_article_set' into field

Change History (14)

comment:1 by tom, 19 years ago

I have the same problem here with latest SVN (rev 2934). It also occures with .count()

Has anybody got a fix?

by curtis.thompson@…, 18 years ago

Attachment: transcript.txt added

Problem transcript

by curtis.thompson@…, 18 years ago

Attachment: models.py added

Simple Model

comment:2 by curtis.thompson@…, 18 years ago

priority: normalhigh
Severity: majorcritical

Attached transcript and simple model showing the problem with self referential ManyToManyField

comment:3 by curtis.thompson@…, 18 years ago

Just another note: The odd thing about this is that when you use the Admin interface, everything works just fine. Very strange.

by curtis.thompson@…, 18 years ago

Attachment: ManyToMany.patch added

Patch files

comment:4 by curtis.thompson@…, 18 years ago

Ok.. Added a patch that fixes the problem for me... Not extensively tested though...

comment:5 by Luke Plant, 18 years ago

Summary: "Cannot resolve keyword ___ into field" error when working with ManyToMany relation[patch] "Cannot resolve keyword ___ into field" error when working with ManyToMany relation
Version: magic-removalSVN

What database are you using? The 'a1.primary_categories.all()' line works fine for me, and I added your model to the m2m_recursive tests and tried it out and it works fine. I've tried using postgres and mysql.

comment:6 by curtis.thompson@…, 18 years ago

This happens when using sqlite3. I'll try MySQL if I get a chance.

comment:7 by anonymous, 18 years ago

Ok. Verified that my 'models.py' attached example fails with MySQL as well (and the patch fixes the problem)

comment:8 by curtis.thompson@…, 18 years ago

More interesting info: Django tests run fine without my patch. The m2m_recursive test fails ('idol' part) with my patch. So the patch is NOT recommended. When I add the 'Person' model to my models, run 'manage.py shell' and manually run the m2m_recursive tests, it fails. So perhaps something isn't being included/initialized properly?

comment:9 by Simon Willison, 18 years ago

I'm seeing the same error, using mysql and a current checkout from SVN:

>>> e.tags.all()
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 88, in __repr__
    return repr(self._get_data())
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 395, in _get_data
    self._result_cache = list(self.iterator())
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 162, in iterator
    select, sql, params = self._get_sql_clause()
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 409, in _get_sql_clause
    tables2, joins2, where2, params2 = self._filters.get_sql(opts)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 540, in get_sql
    tables2, joins2, where2, params2 = val.get_sql(opts)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 589, in get_sql
    return parse_lookup(self.kwargs.items(), opts)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 697, in parse_lookup
    tables2, joins2, where2, params2 = lookup_inner(path, clause, value, opts, opts.db_table, None)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/db/models/query.py", line 800, in lookup_inner
    raise TypeError, "Cannot resolve keyword '%s' into field" % name
TypeError: Cannot resolve keyword 'entry' into field
>>> 

comment:10 by Luke Plant, 18 years ago

This is very strange. From what I know of that exception, is very unlikely to be DB related -- that exception occurs long before any DB calls are made. So I thought maybe it would be Python version -- but i've just tested with Python 2.3, as well as 2.4, with postgres, mysql and sqlite3, and I simply can't reproduce this. I'm guessing it's one of those subtle dict ordering things, or perhaps a mutable default argument type thing, seeing as it doesn't seem to happen with a simple run through of the tests.

I actually noticed lots of mutable default arguments in django.forms, and there are other ones around the place -- such as one in SortedDict, which could easily be relevant. We really need to go through the code and get rid of them all, but there are one or two I'm not sure about -- such as MultiValueDict.__deepcopy__().

Assuming we've followed PEP 8 (which seems to be the case pretty much everywhere), these two commandlines should find most of them:

egrep -r '=\{\}' *
egrep -r '=\[\]' *

I've no idea whether that's actually related, but it's worth sorting out anyway. I'm off to bed now and not likely to be available until Monday, so if someone else wants to do this they won't be treading on my toes :-)

comment:11 by Adrian Holovaty, 18 years ago

Description: modified (diff)

(Fixed formatting in description.)

Note: See TracTickets for help on using tickets.
Back to Top