Search tables related by a foreign key in admin

7 views
Skip to first unread message

Craig Kuhns

unread,
Apr 13, 2007, 2:58:40 PM4/13/07
to django...@googlegroups.com
I am going through the Poll tutorial and i am just messing around with
the code so that i understand what is all going on. While doing this
the great idea occured to me that one should be able to search for a
poll in the admin site based on the choices associated with it. I was
looking around and found something that said I should be able to do
something like this...

search_fields = ['question', 'choice_choice']

Where choice_choice is supposed to be pointing to the choice field in
the choice model, but something is not working with that. I am sure
that I am missing something very easy and I was wondering if someone
could point it out to me.

Thanks,

Craig

RajeshD

unread,
Apr 13, 2007, 4:34:26 PM4/13/07
to Django users

>
> search_fields = ['question', 'choice_choice']
>
> Where choice_choice is supposed to be pointing to the choice field in
> the choice model, but something is not working with that. I am sure
> that I am missing something very easy and I was wondering if someone
> could point it out to me.

Try two underscores:

search_fields = ['question', 'choice__choice']


Craig Kuhns

unread,
Apr 13, 2007, 9:16:47 PM4/13/07
to django...@googlegroups.com
Thanks, that did it.

Craig

Kent Johnson

unread,
May 16, 2007, 11:09:30 AM5/16/07
to django...@googlegroups.com

This doesn't work correctly because it searches the result of an inner
join of the poll and choice tables. Consequently
- it will not find polls that have no choices, even if the search term
is found in the question field, because the polls will not be in the join.
- polls with multiple choices will be listed multiple times in the
result if the search term is found in the question field, because the
poll appears in the join once for each choice.

Is there a way to get this to work correctly?

Thanks,
Kent

Reply all
Reply to author
Forward
0 new messages