[Django] #34090: ordering of ManyToManyField in admin shows objects multiple times

8 views
Skip to first unread message

Django

unread,
Oct 13, 2022, 3:45:34 AM10/13/22
to django-...@googlegroups.com
#34090: ordering of ManyToManyField in admin shows objects multiple times
------------------------------------------------+------------------------
Reporter: spechtx | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.admin | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
I might came across a possible optimization in the admin:

When there is a model with a ManyToManyField and you set this field in the
"ordering" setup in the admin setup, the object will we shown as many
times in the admin, as there are relations stored in the m2m field.

In this example, when the "type" field has 5 relations, the object will be
shown 5 times in the admin.

models.py:


class RelatedM2M(models.Model):
choice = models.CharField(max_length=255, unique=True)
description = models.TextField(blank=True)

class ModelName(models.Model):
id_uuid = models.UUIDField(...)
timestamp_created = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=255)
type = models.ManyToManyField(RelatedM2M, related_name='type')
type_intermediate = models.ManyToManyField(AnotherRelatedM2M,
through='RelatedIntermediate')
geometry = models.GeometryField(...)


admin.py:

@admin.register(ModelName)
classModelNameAdminView(admin.GISModelAdmin):
inlines = (RelatedIntermediateInline,)
autocomplete_fields = ['....']
ordering = ['name', 'type', 'timestamp_created', ]

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

Django

unread,
Oct 13, 2022, 3:46:09 AM10/13/22
to django-...@googlegroups.com
#34090: ordering of ManyToManyField in admin shows objects multiple times
-------------------------------------+-------------------------------------
Reporter: spechtx | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.admin | Version: 4.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by spechtx:

Old description:

> I might came across a possible optimization in the admin:
>
> When there is a model with a ManyToManyField and you set this field in
> the "ordering" setup in the admin setup, the object will we shown as many
> times in the admin, as there are relations stored in the m2m field.
>
> In this example, when the "type" field has 5 relations, the object will
> be shown 5 times in the admin.
>
> models.py:
>

> class RelatedM2M(models.Model):
> choice = models.CharField(max_length=255, unique=True)
> description = models.TextField(blank=True)
>
> class ModelName(models.Model):
> id_uuid = models.UUIDField(...)
> timestamp_created = models.DateTimeField(auto_now_add=True)
> name = models.CharField(max_length=255)
> type = models.ManyToManyField(RelatedM2M, related_name='type')
> type_intermediate = models.ManyToManyField(AnotherRelatedM2M,
> through='RelatedIntermediate')
> geometry = models.GeometryField(...)
>

> admin.py:
>
> @admin.register(ModelName)
> classModelNameAdminView(admin.GISModelAdmin):
> inlines = (RelatedIntermediateInline,)
> autocomplete_fields = ['....']
> ordering = ['name', 'type', 'timestamp_created', ]

New description:

models.py:


admin.py:

--

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

Django

unread,
Oct 13, 2022, 3:47:15 AM10/13/22
to django-...@googlegroups.com

Old description:

New description:

models.py:


admin.py:

autocomplete_fields = [...']


ordering = ['name', 'type', 'timestamp_created', ]
}}}

--

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

Django

unread,
Oct 13, 2022, 3:47:53 AM10/13/22
to django-...@googlegroups.com

Old description:

> admin.py:
>

New description:

models.py:

admin.py:

--

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

Django

unread,
Oct 13, 2022, 4:11:04 AM10/13/22
to django-...@googlegroups.com
#34090: ordering of ManyToManyField in admin shows objects multiple times
-------------------------------------+-------------------------------------
Reporter: spechtx | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: contrib.admin | Version: 4.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

This is a documented behavior, see
[https://docs.djangoproject.com/en/stable/ref/models/querysets/#django.db.models.query.QuerySet.order_by
note] in the `QuerySet.order_by()` docs.

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

Reply all
Reply to author
Forward
0 new messages