On Mar 10, 10:38 pm, Laereom <jamesdbo...@gmail.com> wrote:
> I have two models, we'll call them 'Question' and 'Search'.
>
> Search has a many to many field called 'questions' which contain,
> naturally, a set of questions.
>
> I want to retrieve a Question which is associated with a particular
> Search through that many to many field.
>
> It seemed straightforward -- I wrote something like this:
>
> search = Search.objects.get(myCriteria)
> question = Question.objects.filter(id=search.questions.id)
>
> However, I'm getting an AttributeError:
>
> 'ManyRelatedManager' object has no attribute 'id'
>
> Any advice?

You've used a ManyToMany field, so there isn't a "particular question"
associated with a Search - there are Many, hence the name. So which of
the many questions are you hoping to get?

You can get *all* of them by simply doing:

    search.questions.all()

which returns a queryset, ie a list-like object containing several
questions. See the documentation here:
http://docs.djangoproject.com/en/1.1/ref/models/relations/
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to