Opened 13 years ago

Closed 9 years ago

#16609 closed Bug (fixed)

Admin searches produce duplicate results if search_fields contains M2M traversals

Reported by: cheater00@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords: search_fields m2m duplicate duplicates
Cc: cheater00@…, Timothy Schilling Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

I believe this has already been touched upon in #15819, however the consensus (comment ticket:15819#comment:13) was

"For people in situations where .distinct() is broken for whatever reason, the workaround here is simple: don't use search_fields with relations in a way that triggers it."

I couldn't find a ticket for this, so here it is.

In my case, I am searching for a keyword which is found in a search_fields item that isn't traversing M2M. When search_fields also contains a M2M traversal, I get duplicate results.

In my case - this might be relevant or not - I am traversing a ForeignKey first and then an M2M field in that problematic search_fields entry, e.g:

search_fields = ['=id', '=fkmodel__m2mmodel__name']

This happens even though the string is not matched by fkmodel__m2mmodel__name, only by id.

This might not be clear, this is what the model relations look like:

(Model SearchedModel) --ForeignKey--> (Model FKModel) --ManyToMany--> (Model M2MModel)

M2MModel contains the name field that I have specified in search_fields.

This might or might not be related to #15559.

I have tested this against 1.3, yesterday's 1.3.X, and yesterday's dev and it is broken in all of them. (today is 10 Aug 2011)

Change History (14)

comment:1 by cheater <cheater00@…>, 13 years ago

Cc: cheater00@… added

comment:2 by cheater <cheater00@…>, 13 years ago

Sorry about not using code blocks above - only noticed the error after submitting the ticket. The search_fields should look like this:

search_fields = ['=id', '=fkmodel__m2mmodel__name'] 

comment:3 by Aymeric Augustin, 13 years ago

Description: modified (diff)

Fixed formatting.

comment:4 by Aymeric Augustin, 13 years ago

Description: modified (diff)

comment:5 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

It's likely that this is an ORM bug, but since the symptoms show in the admin, I'll leave it like this until we have more information.

comment:6 by cheater <cheater00@…>, 13 years ago

For one thing, the admin actions also get doubled items. In fact, it is so that this could lead to fairly serious data loss and other errors. Imagine an admin action which bills users for something - they get found twice - and they get billed twice! I think this warrants bumping up the importance. I have also stumbled upon this when I was writing a money-related summary which counted the amount of items in stock, and summarized them, also calculating the prices. The prices were way off, of course. I have done a workaround wich skips objects that have already been seen by tracking the ID, but some other people might not be so lucky as to notice the problem before it's too late.

comment:7 by andreas@…, 11 years ago

Has this bug been fixed in later versions of django? Still broken in 1.3.

comment:8 by Timothy Schilling, 11 years ago

Cc: Timothy Schilling added

This bug has been fixed by at least 1.3.7 and it's fixed in master.

comment:9 by Tim Graham, 11 years ago

Resolution: fixed
Status: newclosed

comment:10 by Samuel Paccoud, 9 years ago

I came across this bug which does not seem to be totally fixed yet? I tried to propose a fix and submitted a PR: https://github.com/django/django/pull/4504

comment:11 by Samuel Paccoud, 9 years ago

Resolution: fixed
Status: closednew

comment:12 by Tim Graham, 9 years ago

Has patch: set

I wonder if we might mark this as a duplicate of #15819. The proposed patch there removes lookup_needs_distinct() instead of making it more complicated and looks like it would fix other issues.

comment:13 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:14 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In d084176c:

Fixed #16609 -- Fixed duplicate admin results when searching nested M2M relations.

This was fixed earlier but only when the M2M relation was at the first
level on the object. This commit fixes the issue even when the M2M is
at deeper levels, such as behind a foreign key.

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