[Django] #16609: Admin searches produce duplicate results if search_fields contains M2M traversals

13 views
Skip to first unread message

Django

unread,
Aug 10, 2011, 5:54:14 AM8/10/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------------------+-------------------------
Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: new
Milestone: | Component:
Version: SVN | contrib.admin
Keywords: search_fields m2m duplicate | Severity: Normal
duplicates | Triage Stage:
Has patch: 0 | Unreviewed
UI/UX: 0 | Easy pickings: 0
-------------------------------------------------+-------------------------
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)

--
Ticket URL: <https://code.djangoproject.com/ticket/16609>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 10, 2011, 5:55:34 AM8/10/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
cheater00@… | Status: new
Type: Bug | Component: contrib.admin
Milestone: | Severity: Normal
Version: SVN | Keywords: search_fields m2m
Resolution: | duplicate duplicates
Triage Stage: | Has patch: 0
Unreviewed | Needs tests: 0
Needs documentation: 0 | Easy pickings: 0
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by cheater <cheater00@…>):

* cc: cheater00@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:1>

Django

unread,
Aug 10, 2011, 5:58:00 AM8/10/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
cheater00@… | Status: new
Type: Bug | Component: contrib.admin
Milestone: | Severity: Normal
Version: SVN | Keywords: search_fields m2m
Resolution: | duplicate duplicates
Triage Stage: | Has patch: 0
Unreviewed | Needs tests: 0
Needs documentation: 0 | Easy pickings: 0
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by cheater <cheater00@…>):

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']
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:2>

Django

unread,
Aug 11, 2011, 6:14:22 PM8/11/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
cheater00@… | Status: new
Type: Bug | Component: contrib.admin
Milestone: | Severity: Normal
Version: SVN | Keywords: search_fields m2m
Resolution: | duplicate duplicates
Triage Stage: | Has patch: 0
Unreviewed | Needs tests: 0
Needs documentation: 0 | Easy pickings: 0
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Old description:

> 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)

New description:

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)

--

Comment (by aaugustin):

Fixed formatting.

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:3>

Django

unread,
Aug 11, 2011, 6:15:17 PM8/11/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
cheater00@… | Status: new
Type: Bug | Component: contrib.admin
Milestone: | Severity: Normal
Version: SVN | Keywords: search_fields m2m
Resolution: | duplicate duplicates
Triage Stage: | Has patch: 0
Unreviewed | Needs tests: 0
Needs documentation: 0 | Easy pickings: 0
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Description changed by aaugustin:

Old description:

> 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)

New description:

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)

--

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:4>

Django

unread,
Aug 11, 2011, 6:16:58 PM8/11/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
cheater00@… | Status: new
Type: Bug | Component: contrib.admin
Milestone: | Severity: Normal
Version: SVN | Keywords: search_fields m2m
Resolution: | duplicate duplicates
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* stage: Unreviewed => Accepted


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:5>

Django

unread,
Aug 12, 2011, 5:24:54 AM8/12/11
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
cheater00@… | Status: new
Type: Bug | Component: contrib.admin
Milestone: | Severity: Normal
Version: SVN | Keywords: search_fields m2m
Resolution: | duplicate duplicates
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by cheater <cheater00@…>):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:6>

Django

unread,
Apr 22, 2013, 3:25:31 AM4/22/13
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------

Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by andreas@…):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:7>

Django

unread,
Sep 21, 2013, 7:06:21 AM9/21/13
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------

Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by CodenameTim):

* cc: CodenameTim (added)


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:8>

Django

unread,
Sep 21, 2013, 7:16:25 AM9/21/13
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed

Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:9>

Django

unread,
Apr 14, 2015, 7:32:06 AM4/14/15
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: cheater00@… | Owner: nobody

Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed
Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by sampaccoud):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:10>

Django

unread,
Apr 14, 2015, 7:32:44 AM4/14/15
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------

Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by sampaccoud):

* status: closed => new
* resolution: fixed =>


--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:11>

Django

unread,
Apr 14, 2015, 8:53:29 AM4/14/15
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------

Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:12>

Django

unread,
Apr 21, 2015, 1:27:17 PM4/21/15
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------

Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:13>

Django

unread,
May 6, 2015, 9:56:50 AM5/6/15
to django-...@googlegroups.com
#16609: Admin searches produce duplicate results if search_fields contains M2M
traversals
-------------------------------------+-------------------------------------
Reporter: cheater00@… | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed

Keywords: search_fields m2m | Triage Stage: Accepted
duplicate duplicates |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"d084176cc1273d5faf6f88eedb4c490e961f3a68" d084176c]:
{{{
#!CommitTicketReference repository=""
revision="d084176cc1273d5faf6f88eedb4c490e961f3a68"
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.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16609#comment:14>

Reply all
Reply to author
Forward
0 new messages