Opened 7 years ago

Last modified 4 years ago

#6933 new New feature

You cannot search with spaces if search_fields is declared with "^"

Reported by: Martín Conte Mac Donell <Reflejo@…> Owned by: diafour
Component: contrib.admin Version: newforms-admin
Severity: Normal Keywords: nfa-someday yandex-sprint ep2008
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by ramiro)

That is. When search_fields is declared to "starts with" you cannot perform queries with spaces.

File contrib/admin/views/main.py; Line 326:

for bit in self.query.split():
    or_queries = [models.Q(**{construct_search(field_name): bit}) for field_name in self.search_fields]
    other_qs = QuerySet(self.model)
    (...)
    qs = qs & other_qs

So, if for example i'm looking for "Hey dud" query will be:

qs.filter(field__startswith="Hey") & qs.filter(field__startswith("dud"))

Attachments (4)

django-6933-0.diff (909 bytes) - added by diafour 7 years ago.
django-6933-0.2.diff (957 bytes) - added by diafour 7 years ago.
django-6933-0.3.diff (892 bytes) - added by diafour 7 years ago.
django-6933-1.diff (1.9 KB) - added by diafour 7 years ago.

Download all attachments as: .zip

Change History (17)

comment:1 Changed 7 years ago by Karen Tracey <kmtracey@…>

  • Keywords nfa-someday added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Code is same here on trunk and nfa, this should not block merge.

comment:2 Changed 7 years ago by ramiro

  • Description modified (diff)

comment:3 Changed 7 years ago by diafour

  • Owner changed from nobody to diafour

Changed 7 years ago by diafour

comment:4 Changed 7 years ago by diafour

  • Has patch set
  • Keywords yandex-sprint added

django-6933-0.diff patch gives ability to use search strings with spaces.

You can define such strings with '"' (like google):

'hey dud' interprets like qs.filter(fieldstartswith="Hey") & qs.filter(fieldstartswith("dud"))

and '"hey dud"' interprets like qs.filter(fieldstartswith="hey dud")

The algorithm is very basic and don't check a string for paired quotes. So '"hey dud' works like '"hey dud"'.

Changed 7 years ago by diafour

Changed 7 years ago by diafour

comment:5 Changed 7 years ago by diafour

Fix tabs in a patch.

comment:6 Changed 7 years ago by Honza_Kral

  • Needs documentation set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

Please take a look at django.utils.text.smart_split for the splitting tool. And document this feature.

comment:7 Changed 7 years ago by diafour

New patch use smart_split from django.utils.text.

Changed 7 years ago by diafour

comment:8 Changed 7 years ago by diafour

django-6933-1.diff patch updated with addition to docs/model-api.txt

comment:9 Changed 7 years ago by Honza_Kral

  • Keywords ep2008 added
  • Needs documentation unset
  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

thanks diafour. I just think that the doc patch should reffer to docs/admin.txt, but that's probably just a minor glitch

comment:10 Changed 7 years ago by russellm

  • Needs tests set
  • Triage Stage changed from Ready for checkin to Accepted

comment:11 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Uncategorized

#15203 was closed as a dupe. It reports the same problem but when the field name starts with =. It has a much more current patch too.

comment:12 Changed 4 years ago by julien

  • Type changed from Uncategorized to Bug

comment:13 Changed 4 years ago by kmtracey

  • Easy pickings unset
  • Needs documentation set
  • Type changed from Bug to New feature
  • UI/UX unset

#17039 reported this again. Also given the current behavior is documented (https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields) I'd call this ticket a new feature, not a bug. Also, I don't see that the doc mentioning the inability to search exactly on phrases with spaces is changed by the newest patch here so I think there is some doc change needed in addition to what is added by the patch.

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