How to Query objects.filter ForeignKey

48 views
Skip to first unread message

easypie

unread,
Jun 26, 2012, 1:55:21 PM6/26/12
to django...@googlegroups.com
I'm trying to pass a objects.filter dictionary to HttpResponse. The problem is I have 2 models: InfoBox and InfoDetail. InfoDetail is linked to InfoBox by models.ForeignKey(InfoBox). What I want to do is filter by title which lives in the InfoBox model. I performed infobox = InfoDetail.objects.filter(title=info_title), but it returns an error cus 'title' doesn't live in InfoDetail.

How can i grab and filter it by title and pass {'infobox': infobox} in HttpResponse when HttpRequest is called?

Kurtis Mullins

unread,
Jun 26, 2012, 4:04:40 PM6/26/12
to django...@googlegroups.com
 I performed infobox = InfoDetail.objects.filter(title=info_title), but it returns an error cus 'title' doesn't live in InfoDetail.

How can i grab and filter it by title and pass {'infobox': infobox} in HttpResponse when HttpRequest is called?

If you want to filter by a certain title, then you'd do something like this:

InfoDetail.objects.filter(infobox__title="Some Title") 

That would return all InfoDetail objects whose related InfoBox has a specific title.

If that doesn't help solve your problem, let me know what else you're stuck on :)

easypie

unread,
Jun 26, 2012, 5:16:04 PM6/26/12
to django...@googlegroups.com
That worked like a charm! I read about the double underscore but didn't think much about it when it crossed my mind. I've been grinding on this for the whole day. My eyes... =)
Reply all
Reply to author
Forward
0 new messages