Opened 15 years ago

Closed 15 years ago

Last modified 12 years ago

#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)

testcase.diff (3.8 KB ) - added by Zain Memon 15 years ago.
Failing testcase
10516_fix.diff (5.4 KB ) - added by Zain Memon 15 years ago.
fix + tests

Download all attachments as: .zip

Change History (13)

comment:1 by Jacob, 15 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 by cbess, 15 years ago

Owner: changed from nobody to cbess

comment:3 by anonymous, 15 years ago

Owner: cbess removed

won't have time to finish

comment:4 by Zain Memon, 15 years ago

Owner: set to Zain Memon
Status: newassigned

by Zain Memon, 15 years ago

Attachment: testcase.diff added

Failing testcase

comment:5 by Zain Memon, 15 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 Alex Gaynor, 15 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 Zain Memon, 15 years ago

Has patch: set
Resolution: fixed
Status: assignedclosed

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 Zain Memon, 15 years ago

Resolution: fixed
Status: closedreopened

by Zain Memon, 15 years ago

Attachment: 10516_fix.diff added

fix + tests

comment:9 by Jacob, 15 years ago

Triage Stage: AcceptedReady for checkin

comment:10 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: reopenedclosed

(In [10684]) Fixed #10516 -- Corrected admin search when the search_fields definition contains multiple fields on the same base model. Thanks to Zain Memon for the patch.

comment:11 by Jacob, 12 years ago

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top