Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#29915 closed Cleanup/optimization (fixed)

Allow icontains lookup to accept uuids with or without dashes

Reported by: Václav Řehák Owned by: Ian Foote
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: uuid 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

We have Django 2.1 project with model admin which includes an UUIDField in list_display and search_fields. The UUID is displayed with dashes on changelist (e.g. "245ba2eb-6852-47be-82be-7dc07327cf9e") and if the user cut'n'paste it to the search field, I would expect admin to find it.

This works however only on Postgres but fails on Oracle. I can understand why this happens (Oracle backend stores uuid as string) and I believe I can workaround it by customizing get_search_results but I think should be internal thing that Django handles gracefully - search should be possible by the value as displayed in admin.

Change History (12)

comment:1 by Tim Graham, 6 years ago

Component: contrib.adminDatabase layer (models, ORM)
Summary: Admin search for UUID with dashes works only on PostgresAllow icontains lookup to accept uuids with or without dashes
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

This isn't really an admin issue but rather it's due to the fact that the default admin lookup uses __icontains. You could fix the issue by using search_fields = ['uuidfield__exact'] (adding __exact) although that doesn't allow searching for part of the UUID value.

I'll tentatively accept the ticket to allow QuerySet.objects.filter(uuidfield__icontains='...') to work with values with or without dashes

comment:2 by Václav Řehák, 6 years ago

Thank you for the quick answer. However, the proposed solution does not work as changing the uuid lookup to exact causes an error when searching for any string other than valid uuid value.

I get ValidationError raised from UUIDField.to_python()

File "/home/vaclav/.local/share/virtualenvs/aaa-yRfablWV/lib/python3.7/site-packages/django/db/models/fields/__init__.py" in to_python
  2325.                 return uuid.UUID(value)

File "/usr/lib/python3.7/uuid.py" in __init__
  160.                 raise ValueError('badly formed hexadecimal UUID string')

comment:3 by Ian Foote, 6 years ago

Owner: changed from nobody to Ian Foote
Status: newassigned

comment:4 by Ian Foote, 6 years ago

Has patch: set
Version 0, edited 6 years ago by Ian Foote (next)

comment:5 by Carlton Gibson, 6 years ago

Patch needs improvement: set
Version: 2.1master

comment:6 by Ian Foote, 5 years ago

Patch needs improvement: unset

comment:7 by Mariusz Felisiak, 5 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 485f65b:

Refs #29915 -- Added tests for using pattern lookups with values without hyphens for UUIDField.

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 343afa7:

Refs #29915 -- Doc'd limitation of using pattern lookups with UUIDField on PostgreSQL.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In d9881a02:

Fixed #29915 -- Added support for values with hyphens to pattern lookups for UUIDField on backends without UUID datatype.

Support hyphens in iexact, contains, icontains, startswith, istartswith,
endswith and iendswith UUIDField filters on backends without UUID
datatype.

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 368832e:

[3.0.x] Refs #29915 -- Doc'd limitation of using pattern lookups with UUIDField on PostgreSQL.

Backport of 343afa788080fb874bcd10eab1a1a2fede98c526 from master

comment:12 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In ae2dee6e:

[2.2.x] Refs #29915 -- Doc'd limitation of using pattern lookups with UUIDField on PostgreSQL.

Backport of 343afa788080fb874bcd10eab1a1a2fede98c526 from master

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