ManyToMany "Cannot resolve keyword"

53 views
Skip to first unread message

NakedHTML

unread,
Oct 27, 2006, 9:55:03 PM10/27/06
to Django users
I've been looking through the archives here and goggeling around trying
to resolve what I am sure is a simple problem.

I am trying to do the following:

1) Display an article
2) Display the reporter (many to many field)
3) Display related images (many to many field)

The model for the complete app is here: http://paste.e-scribe.com/2407/

I am trying to do in the template is this:

{% for reporter in object.reporter_byline.all %}
{{ reporter.user.first_name }}
{% endfor %}

This randomly breaks. Sometimes it does work and the username is
displayed and other times it'll break with
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py
in lookup_inner, line 836

Running on MAC OS X Tiger, python 2.4, django latest svn version, mysql
5, modpython, apache 2.2

My question may be juvenile, or it may have been answered elsewhere.
Any direction or pointing in the right direction is appreciated.

Malcolm Tredinnick

unread,
Oct 27, 2006, 10:45:54 PM10/27/06
to django...@googlegroups.com
On Sat, 2006-10-28 at 01:55 +0000, NakedHTML wrote:
> I've been looking through the archives here and goggeling around trying
> to resolve what I am sure is a simple problem.
>
> I am trying to do the following:
>
> 1) Display an article
> 2) Display the reporter (many to many field)
> 3) Display related images (many to many field)
>
> The model for the complete app is here: http://paste.e-scribe.com/2407/
>
> I am trying to do in the template is this:
>
> {% for reporter in object.reporter_byline.all %}
> {{ reporter.user.first_name }}
> {% endfor %}
>
> This randomly breaks. Sometimes it does work and the username is
> displayed and other times it'll break with
> /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py
> in lookup_inner, line 836

We may need a bit more information there. It sounds like (from the title
of this email, which isn't mentioned anywhere else here) like one of the
fields you are expecting to be non-null is empty and so the related
instance doesn't exist and you can't query against it. However, it's
very hard to tell.

Your code paste is over 500 lines long, so I'm not going to be able to
spot the error just by eye-balling it without devoting hours to the
problem. If I were you, I would start by trying to trim down you failure
to something as small as possible that fails. For example, which of the
above two lines in your template is triggering the problem? If you
remove the reporter.user.first_name line and replace it with a
hard-coded string, do you still see the problem (which would indicate it
is the first line)?

You might also want to put the {% debug %} tag in your template and then
view the resulting HTML source to see what the contents of your various
objects are and whether they contain what you expect. Note that when
using {% debug %}, it is important to "view source", because Python uses
angle brackets for some of its output and this will be swallowed by the
HTML rendering process which will throw away unknown elements. Using the
debug output like this, you might be able to work out which particular
value of "object" causes the problem and then inspect your database (and
use "python manage.py shell") to do some investigations to see what
results are really being returned.

If none of this helps, reduce your template just to those two lines.
Verifies that it still fails. Then start removing code from your models
until things start working again. What was the last thing you removed?
Why did it change things?

Hope that gives you some clues to move forwards a little bit.

Regards,
Malcolm


NakedHTML

unread,
Oct 27, 2006, 11:03:57 PM10/27/06
to Django users
Malcolm Tredinnick wrote:

> We may need a bit more information there. It sounds like (from the title
> of this email, which isn't mentioned anywhere else here) like one of the
> fields you are expecting to be non-null is empty and so the related
> instance doesn't exist and you can't query against it. However, it's
> very hard to tell.

Malcom, thanks for the help!

I suspect that paragraph above is where the problem lies. I'll vaugly
remember changing some of the blank/null options a week or so back
--around the time my troubles started. I'll whittle it down and report
back.

Regards,

Johnny

NakedHTML

unread,
Oct 28, 2006, 12:59:38 PM10/28/06
to Django users
Okay, I trimmed things down a bit and put the models into a new
project. Models -> http://paste.e-scribe.com/2425/

I'm trying to access the reporter (or reporters) of the story in the
template using -> http://paste.e-scribe.com/2424/

Same as before, this randomly works and errors out with ->
http://paste.e-scribe.com/2423/

Am I completly off track on trying to access a manytomany field this
way?

Jay Parlar

unread,
Oct 28, 2006, 1:26:26 PM10/28/06
to django...@googlegroups.com

Is there any chance that you've got some other URL accidentally using
whatever view causes this template to get rendered?

My initial guess would be that the template is being rendered with
some 'object', but the 'object' is not in fact a 'Story' object, as is
required.

And definitely try Malcolm's suggestion of using {% debug %} in the template.

Jay P.

samulih

unread,
Oct 28, 2006, 2:16:32 PM10/28/06
to Django users

My wild guess: things get broken, when you update some of your source
files and don't restart server. This happens to me regularly and the
error messages match also.

- samuli

NakedHTML

unread,
Oct 28, 2006, 6:32:20 PM10/28/06
to Django users
Thanks to everyone for your help and support!

As it turns our, Malcoms advice of:

>Then start removing code from your models until things start working again.

...was what I needed. It gave me a chance to wade through the models
and find the offending code.

Looks like it was line 84 of the origional code ->
http://paste.e-scribe.com/2407/
professor = models.ForeignKey(User,choices = [(user.id,
user.get_full_name() or user.username) for user in
User.objects.filter(groups__name__iexact="Faculty").filter(is_active__iexact="1")],
related_name="professor", verbose_name="Faculty" )

I changed this to something that was already working elsewhere:
professor = models.ForeignKey('UserProfile', related_name="professor",
limit_choices_to={'user__is_active__iexact' : '1' ,
'user__groups__name__iexact' : 'Faculty'}, verbose_name="Professor")

I also changed a few field names in the Story model and model names
elsewhere. The working (right now, anyway) models.py can be found at
http://paste.e-scribe.com/2429/

Thanks! jd

Reply all
Reply to author
Forward
0 new messages