Cannot resolve keyword into field

27 views
Skip to first unread message

Ramashish Baranwal

unread,
Apr 21, 2007, 6:13:41 PM4/21/07
to Django users
Hi,

I using a script to fetch some database records. However, when it
encounters a ManyToManyField it gives the following error-

TypeError: Cannot resolve keyword 'book' into field

My models.py looks like this-

from django.db import models

# Create your models here.
class Author(models.Model):
name = models.CharField(maxlength=100)

class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)

and my test script to retrieve records contains (the app name is
test)-

import os
if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import settings
from test.models import *

b = Book.objects.get(id=1)
for a in b.authors.all(): print a

I am using django with sqlite2 on Fedora Core 6 with python-2.4.3 and
have tried version 0.95, 0.96 and svn trunk. All throw the same error-
TypeError: Cannot resolve keyword 'book' into field
When I try the same through "python manage.py shell" interactive
shell, i.e. by entering the two lines above, it works fine. Probably,
I'm missing some setting option. But the same script works for models
that don't contain an M2M field, so that seems unlikely.

Any clues?

Thanks,
Ram

Ramashish Baranwal

unread,
Apr 22, 2007, 12:59:44 AM4/22/07
to Django users
On Apr 22, 3:13 am, Ramashish Baranwal <ramashish.li...@gmail.com>
wrote:

Sorry, a typo in my earlier post. Its sqlite3 not sqlite2.

Ram

carole...@gmail.com

unread,
Apr 22, 2007, 10:33:47 AM4/22/07
to Django users
Can you post the exact code throwing the error, and copy paste your
exact model code related to the error?

It sounds like you have something like the following:

.filter(book=

where it should be something like

.filter(someobject__book

But it's hard to tell w/out seeing the code.

On Apr 22, 12:59 am, Ramashish Baranwal <ramashish.li...@gmail.com>

Ned Batchelder

unread,
Apr 23, 2007, 11:59:22 AM4/23/07
to django...@googlegroups.com
FWIW, I just submitted a patch which makes that error message more helpful by showing the list of valid fields: http://code.djangoproject.com/ticket/4130

--Ned.
-- 
Ned Batchelder, http://nedbatchelder.com

Jason McVetta

unread,
Apr 23, 2007, 4:45:00 PM4/23/07
to django...@googlegroups.com
On 4/21/07, Ramashish Baranwal <ramashi...@gmail.com> wrote:
  TypeError: Cannot resolve keyword 'book' into field


This is a long-standing, well-known bug that apparently no one (including me) knows how to fix. 

Any time one defines a ManyToMany relationship, then calls all() on that relationship from a script, a "cannot resolve keyword into field" error results.  The problem involves some deep voodoo about how and when modules are imported, which is why it occurs only in scripts but not in the admin interface or the manage.py shell.  If you google around the django-users archives and bug tickets, you'll see some (imho truly hideous) hacks for working around it by mangling your import statements.

Ramashish Baranwal

unread,
Apr 24, 2007, 8:04:27 AM4/24/07
to Django users
On Apr 22, 7:33 pm, "carole.zie...@gmail.com"

<carole.zie...@gmail.com> wrote:
> Can you post the exact code throwing the error, and copy paste your
> exact model code related to the error?

The code is exactly the same as I wrote in my OP.

# models.py

from django.db import models

# Create your models here.
class Author(models.Model):
name = models.CharField(maxlength=100)

class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)

# test.py, this is used to retrieve author records of a particular
book.


import os
if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import settings
from test.models import *

b = Book.objects.get(id=1)
for a in b.authors.all(): print a

>


> It sounds like you have something like the following:
>
> .filter(book=
>
> where it should be something like
>
> .filter(someobject__book
>
> But it's hard to tell w/out seeing the code.
>

I am trying to get authors of a particular book, so
b = Book.objects.get(id=1) # Get some book
# Get the authors


for a in b.authors.all(): print a

However, as Jason mentioned below, it seems to be an unsolved bug. :(

-Ram

Jason McVetta

unread,
Apr 24, 2007, 5:45:44 PM4/24/07
to django...@googlegroups.com
On 4/24/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
I now have to sit down and work out a proper solution. That will take
time and I don't have that at the moment because I have other
priorities. Of course, anybody else should feel free to dive in and
write a fix, too, but that hasn't happened so far.

I would be happy to write a fix; but unfortunately, I do not yet understand the bug well enough to do so.


Until then, anybody being bitten by the problem might want to try Ben
Slavin's workaround patch in #1796.

Ben posted patches for the management shell and for mod_python.  The case where I am experiencing this bug is running a script from the command line (or from cron).  I tried including his work around code
# XXX: (Temporary) workaround for ticket #1796: force early loading of all
# models from installed apps.
from django.db.models.loading import get_models
loaded_models = get_models()
at the beginning of my script, but it did not help the problem. 


This is a bit of an oversimplification, since it occurs more with
reverse relationships and it's not "any time", it's only sometimes and
depends on the code (model names, etc), OS, installation details and
which deployment method you're running under. Typically the reverse to
what you describe occurs: a problem like this is more likely to show up
in the admin interface or when running under mod_python.

Interesting -- maybe it's because I am running from svn rather than a release, but I personally have not experienced this bug in the admin interface. 


Benjamin Slavin

unread,
Apr 24, 2007, 10:29:54 PM4/24/07
to django...@googlegroups.com
On 4/24/07, Jason McVetta <jason....@gmail.com> wrote:
> Ben posted patches for the management shell and for mod_python. The case
> where I am experiencing this bug is running a script from the command line
> (or from cron). I tried including his work around code
...

> at the beginning of my script, but it did not help the problem.

Jason,

I've posted a modified script of what I'm using internally for a
script that had encountered this problem. [0] I'll be curious to hear
if it this helps you out at all.

- Ben

[0] http://dpaste.com/hold/9014/

Ramashish Baranwal

unread,
Apr 24, 2007, 8:09:02 AM4/24/07
to Django users

Jason, I have observed that if I add the directory containing my
django project to PYTHONPATH, this problem doesn't come (something
similar was mentioned in one of the tickets). For example if my
project resides in /home/ram/mysite-, adding this line at the top of
my script make it work-

sys.path.append('/home/ram')

May be, that can provide some hint.

-Ram

Jason McVetta

unread,
Apr 25, 2007, 11:15:29 AM4/25/07
to django...@googlegroups.com
On 4/24/07, Benjamin Slavin <benjami...@gmail.com> wrote:
I've posted a modified script of what I'm using internally for a
script that had encountered this problem. [0]  I'll be curious to hear
if it this helps you out at all.

Ben, thank you very much for the script -- it fixed my problem!


Reply all
Reply to author
Forward
0 new messages