Opened 9 years ago

Last modified 5 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 Morales)

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 8 years ago.
django-6933-0.2.diff (957 bytes) - added by diafour 8 years ago.
django-6933-0.3.diff (892 bytes) - added by diafour 8 years ago.
django-6933-1.diff (1.9 KB) - added by diafour 8 years ago.

Download all attachments as: .zip

Change History (17)

comment:1 Changed 8 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 8 years ago by Ramiro Morales

Description: modified (diff)

comment:3 Changed 8 years ago by diafour

Owner: changed from nobody to diafour

Changed 8 years ago by diafour

Attachment: django-6933-0.diff added

comment:4 Changed 8 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 8 years ago by diafour

Attachment: django-6933-0.2.diff added

Changed 8 years ago by diafour

Attachment: django-6933-0.3.diff added

comment:5 Changed 8 years ago by diafour

Fix tabs in a patch.

comment:6 Changed 8 years ago by Honza Král

Needs documentation: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

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

comment:7 Changed 8 years ago by diafour

New patch use smart_split from django.utils.text.

Changed 8 years ago by diafour

Attachment: django-6933-1.diff added

comment:8 Changed 8 years ago by diafour

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

comment:9 Changed 8 years ago by Honza Král

Keywords: ep2008 added
Needs documentation: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady 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 8 years ago by Russell Keith-Magee

Needs tests: set
Triage Stage: Ready for checkinAccepted

comment:11 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: 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 5 years ago by Julien Phalip

Type: UncategorizedBug

comment:13 Changed 5 years ago by Karen Tracey

Easy pickings: unset
Needs documentation: set
Type: BugNew 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