#10516 closed (fixed)
Admin search doesn't work when having multiple search_fields to the same base model.
| Reported by: | Anssi Kääriäinen | Owned by: | Zain Memon |
|---|---|---|---|
| Component: | contrib.admin | Version: | 1.0 |
| Severity: | Keywords: | search, inheritance, admin | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Searching in admin site does not work when search_fields has multiple elements from the same base class. This is best described by an example:
admin.py:
from testi.searchTest.models import *
from django.contrib import admin
class TitleInline(admin.TabularInline):
model = TitleTranslation
extra = 2
class RecommenderAdmin(admin.ModelAdmin):
inlines = [TitleInline]
class RecommendationAdmin(admin.ModelAdmin):
inlines = [TitleInline]
# This works
search_fields = ('recommender__titletranslation__text', )
# But this doesn't
# search_fields = ('titletranslation__text', 'recommender__titletranslation__text',)
admin.site.register(Recommendation, RecommendationAdmin)
admin.site.register(Recommender, RecommenderAdmin)
models.py:
from django.db import models
class Title(models.Model):
def __unicode__(self):
try:
return self.titletranslation_set.filter(lang="FI")[0].text
except:
return "No finnish name defined!"
LANG_CHOICES = (('FI', 'Finnish'), ('EN', 'English'),)
class TitleTranslation(models.Model):
title = models.ForeignKey(Title)
text = models.CharField(max_length = 100)
lang = models.CharField(max_length = 2, choices = LANG_CHOICES)
class Recommender(Title):
pass
class Recommendation(Title):
recommender = models.ForeignKey(Recommender)
Assume we have saved a recommendation with a title of 'Foo' and a recommender with a title of 'Bar'. The foreign key is set from 'Foo' to 'Bar'.
In the example above, when searching for 'ar' nothing is found when using the second version of search_fields. When using the first version, The 'Foo' recommendation is found correctly. In the second version, searching works correctly through titletranslationtext.
Attachments (2)
Change History (13)
comment:1 by , 17 years ago
| milestone: | → 1.1 |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 17 years ago
| Owner: | changed from to |
|---|
comment:3 by , 17 years ago
| Owner: | removed |
|---|
comment:4 by , 17 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:5 by , 17 years ago
It looks like this might be caused by an ORM bug. Given two querysets q1 and q2, with list(q1) = [foo] and list(q2) = [foo], there's a case when q1 & q2 = [].
I created a quick test to demonstrate the case. It's using the models from your example verbatim, but I'll clean it up once I've fixed the bug.
comment:6 by , 17 years ago
We really should be able to reproduce this without poking at internals(which is really want dupe select related is, lack of leading underscore be damned).
comment:7 by , 17 years ago
| Has patch: | set |
|---|---|
| Resolution: | → fixed |
| Status: | assigned → closed |
I'll bring up the (possible) ORM bug on the mailing list as a separate bug. It's actually pretty easy fix this bug without hitting the other one. Fix incoming.
comment:8 by , 17 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
comment:9 by , 17 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:10 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
won't have time to finish