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
Sorry, a typo in my earlier post. Its sqlite3 not sqlite2.
Ram
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, http://nedbatchelder.com
TypeError: Cannot resolve keyword 'book' into field
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
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.
Until then, anybody being bitten by the problem might want to try Ben
Slavin's workaround patch in #1796.
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.
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
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
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.