Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#10516 closed (fixed)

Admin search doesn't work when having multiple search_fields to the same base model.

Reported by: akaariai Owned by: zain
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: UI/UX:

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 6 years ago.
Failing testcase
10516_fix.diff (5.4 KB) - added by zain 6 years ago.
fix + tests

Download all attachments as: .zip

Change History (13)

comment:1 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by cbess

  • Owner changed from nobody to cbess

comment:3 Changed 6 years ago by anonymous

  • Owner cbess deleted

won't have time to finish

comment:4 Changed 6 years ago by zain

  • Owner set to zain
  • Status changed from new to assigned

Changed 6 years ago by zain

Failing testcase

comment:5 Changed 6 years ago by zain

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 6 years ago by Alex

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 6 years ago by zain

  • Has patch set
  • Resolution set to fixed
  • Status changed from assigned to 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 Changed 6 years ago by zain

  • Resolution fixed deleted
  • Status changed from closed to reopened

Changed 6 years ago by zain

fix + tests

comment:9 Changed 6 years ago by jacob

  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from reopened to closed

(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 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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