Opened 10 years ago

Closed 10 years ago

Last modified 7 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 10 years ago.
Failing testcase
10516_fix.diff (5.4 KB) - added by Zain Memon 10 years ago.
fix + tests

Download all attachments as: .zip

Change History (13)

comment:1 Changed 10 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 Changed 10 years ago by cbess

Owner: changed from nobody to cbess

comment:3 Changed 10 years ago by anonymous

Owner: cbess deleted

won't have time to finish

comment:4 Changed 10 years ago by Zain Memon

Owner: set to Zain Memon
Status: newassigned

Changed 10 years ago by Zain Memon

Attachment: testcase.diff added

Failing testcase

comment:5 Changed 10 years ago by Zain Memon

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 Changed 10 years ago by Alex Gaynor

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 Changed 10 years ago by Zain Memon

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 Changed 10 years ago by Zain Memon

Resolution: fixed
Status: closedreopened

Changed 10 years ago by Zain Memon

Attachment: 10516_fix.diff added

fix + tests

comment:9 Changed 10 years ago by Jacob

Triage Stage: AcceptedReady for checkin

comment:10 Changed 10 years ago by Russell Keith-Magee

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 Changed 7 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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